In this post I describe how to create a custom SharePoint Event Receiver using the Visual Studio Extensions for SharePoint Services.
Windows Server 2003/2008
SharePoint Services 3.0/Server 2007
Visual Studio 2005/2008
Visual Studio Extensions für SharePoint Services
Download Visual Studio Extensions
We will develop an event receiver that prevents documents from being deleted by cancelling all delete operations.
Step 1: Set up visual studio project
Open Visual Studio and create an „Empty“ SharePoint Project.
Weg got a strong name key to sign the assembly that will be deployed to the global assembly cache (GAC), some SharePoint references and two code files and xml files.
The ItemEventReceiver class contains all events that will be fired when something happens to list items like adding, updating or deleting of files.
The ListEventReceiver class contains all events that will be fired when something happens to the list itself like adding or removing columns
Step 2: Add event code
We want to cancel delete operations so we have to implement an event in the ItemEventReceiver class. Open it and uncomment the ItemDeleting event.
/// Synchronous before event that occurs before an existing item is completely deleted.
/// <param name="properties">
/// A Microsoft.SharePoint.SPItemEventProperties object that represents properties of the event handler.
public override void ItemDeleting(SPItemEventProperties properties)
properties.Cancel = true;
properties.ErrorMessage = "Deleting of Items not allowed!";
Build the project to check for errors, there shouldn’t be any.
Step 3: Deploy
Visual Studio can deploy the event receiver for you. Just select Build –> Deploy from the menu.
Visual Studio creates one feature for each of your event receivers and uses the SharePoint console to check for errors. If there are no errors visual studio creates a deployable solution package (wsp) and a setup.bat script that can be used to deploy and undeploy the solution. Finally visual studio executes the setup.bat.
Your event receiver is now deployed and activated on the root web site, not on sub sites.
If you want to test te event receiver on a sub site you have to activate it first: Navigate to Site Settings -> Site Feature and activate the feature MyEventReceiverItemEventReceiver.
(I’ll show how to change the receiver name later in this article.)
Step 4: Test
Pick an existing document library on the site or create a new one. Upload a document and try to delete it. You should receive a message like this:
If you deactivate the feature MyEventReceiverItemEventReceiver you will be able to delete the file.
Step 5: Feature description
To give the feature a more meaningful description we can modify the feature.xml. To find this file make invisible files visible to your solution explorer:
Discover the pkg folder and open the file MyEventReceiverItemEventReceiver\feature.xml
You can modify the Title attribute and add a Description attribute:
<?xml version="1.0" encoding="utf-8"?>
Description="event receiver that prevents doclib files from being deleted."
<ElementManifest Location="MyEventReceiverItemEventReceiver\ItemEventReceiver.xml" />
Now rebuild and deploy your project. In your SharePoint site navigate to Site Settings -> Site features. Here you see the new description so your SharePoint administrators can understand what’s this feature is about.
The event receiver is attached to all document libraries of the site where the feature is activated.
If you want to attach the Event Receiver to one single list you have to code this with a custom
Feature Event Receiver or use this tool: