Protecting SharePoint lists against deleteion!

That was a problem that we encountered in one of our projects. We needed to prevent some lists and libraries from being deleted even by site collection adminsitartors.

Actually there are two ways to do that :

1) Using Event Handlers
Firstly I though of extending SPListEventReceiver, I expected the presence of a function called “ListDeleting”. Unfortunately, there is no such thing as “ListDeleting”.

Ali Al-Kahki ( My ITWorx colleague ) came up with a very nice workaround listed below :

public override void ItemDeleting(SPItemEventProperties properties)
{
if (properties.ListItemId == 0)
{
properties.Cancel = true;
properties.ErrorMessage = “You are not allowed to delete the list”;
}
}

2) Using SPList.AllowDeletion

When I was exploring the SharePoint object model, I discovered a very interesting property in the SPList class. When setting this property to false, the “Delete list” option will not be displayed in the list settings page .Even if you try to delete the list programmatically an exception will be thrown as long as this property is set to false.

SPList list = web.GetList(web.ServerRelativeUrl+”/Lists/ListName”);
list.AllowDeletion = false;
list.update();

I prefer the second approach but I like Ali’s workaround 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s