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.
First Option: 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.
Second Option: 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.
So a problem is we do not have any event called ListDeleting or ListDeleted. So how to handle this situation.
First Option: 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;
Second Option: 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 ;
}
}
No comments:
Post a Comment