Friday, April 17, 2009

How to Handle / prevent List deletion from site

Hi All,

In our project there was a requirement where few lists once created, has to be there. They can not be deleted. Even not by the site administrator.

So a problem is we do not have any event called ListDeleting or ListDeleted. So how to handle this situation.

I dig a bit with the properties of list and document library and found something that I would like to bring it for sharing with you all.

Two ways I found. One is not to give any option for deleting list. Means options that we find in permissions and management in list settings, we don’t display that option at all.

Ok, how to achieve it. Simple. I found one property which is interesting of List and document library. Name of the property is AllowDeletion. Set it to false and update the list. The option for Delete this list is gone.!!!!

This is just sample code for this.

SPWeb objWeb = SPContext.Current.Web;

SPList objList = objWeb.Lists["{List }"];

objWeb.AllowUnsafeUpdates = true;

objList.AllowDeletion = false;


objWeb.AllowUnsafeUpdates = false;

Now what about the second option. Ok, one more interesting part. As we do not have event handler like ListDeleting but we do have ItemDeleting. So what we can do is create one event handler class, attach to the list that you do not want to be deleted by anybody and write down some code stuff like this and even you can capture details like who tried to delete the list. Capture those data and add it in to your other custom list or database or what ever media that you prefer.

Here is interesting thig is when you delete the list, then also ItemDeleting will be called and you will get ListItemId as 0 because you are not deleting any Item from list.

public override void ItemDeleting(SPItemEventProperties properties)

int itemid = properties.ListItemId;

if (itemid == 0)
properties.Cancel = true;
properties.ErrorMessage = "You are not allowed to delete the
string strLoggedInUserWhoTriedToDelete = properties.UserLoginName ;



That is it. You are done with your job…!!!

1 comment:

Anonymous said...

Hi there, I was just wondering about the 2nd option. You are inheriting from the class SPListItemReceiver (which is for SPListItems) then you are trying to handle SPLIst deletion.

Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to