First published on TECHNET on Sep 07, 2012
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:
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.
Modify the above code to have the correct assembly, class and public key token.
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!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.