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)
properties.Status = SPEventReceiverStatus.CancelWithError;
properties.ErrorMessage = "This site cannot be deleted; Contact your administrator";
properties.Cancel = true;
Above code does the job of preventing site deletion. The error message gets displayed in the error page displayed.
Now, in order to turn on/off this functionality, we need to add a feature receiver. Right click on Feature1 in your project and add an event receiver.
Modify the Feature activated and deactivating events as below to attach/detach the web event receiver created in the previous section.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
SPWeb web = properties.Feature.Parent as SPWeb;
SPEventReceiverDefinition siteDeletingReceiver = web.EventReceivers.Add();
siteDeletingReceiver.Class = "MySiteEventReceiver.EventReceiver1.EventReceiver1";
siteDeletingReceiver.Assembly = "MySiteEventReceiver, Version=18.104.22.168, Culture=neutral, PublicKeyToken =3cb47bda8f8a66de";
siteDeletingReceiver.SequenceNumber = 3000;
siteDeletingReceiver.Type = SPEventReceiverType.SiteDeleting;
Modify the above code to have the correct assembly, class and public key token.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
foreach (SPEventReceiverDefinition ev in web.EventReceivers)
Above code deletes the WebDeleting event receiver when the feature is activated. You can find the name of the event receiver in the element.xml part of the project.
This is how the error page appears, when somebody attempts to delete a site.
Sample visual studio solution is available for download
Hope you found this post helpful. Happy coding!!
Good to see sharepoint. event recievers.....................
Only a particular sharepoint group must not be able to delete the site .How do i customize this ?
Greate blog post. Very effective implimentation of event receiver