This post is a contribution from Charls Tom Jacob, an engineer with the SharePoint Developer Support team.
In this blog I will describe how to develop a custom site event receiver to prevent users from deleting a SharePoint site. By default, any user with the sufficient permission can delete a site. This custom solution can be implemented as an extra check to prevent users from knowingly or unknowingly delete a site.
SharePoint provides event receivers at site collection, web, list, item, and field levels to handle various scenarios like create, delete, update etc. In our case, objective is to prevent a user from deleting a web.
In order to achieve this, we need to build to components:
1. A Web event receiver
2. A Feature event receiver
Web event receiver is to handle the “WebDeleting” event which is fired (like any other “ing” event) when users attempts to delete a site, and before the site is actually deleted.
Feature event receiver is to control this functionality; when the feature is activated, it registers the web event receiver to the web in context, ultimately preventing users from deleting the site. Deactivating the feature removes the web event receiver from the web, thereby allowing a site to be deleted as usual.
Now, let’s see how we can build this solution using Visual Studio 2010. Start visual studio and create a new event receiver project. Select the event receiver settings as below:
Replace the WebDeleting event with the following code:
/// A site is being deleted.
public override void WebDeleting(SPWebEventProperties properties)