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;
objList.Update();
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
list";
string strLoggedInUserWhoTriedToDelete = properties.UserLoginName ;
}
}
That is it. You are done with your job…!!!
1 comment:
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.
Post a Comment