In this post I describe how to create a custom SharePoint Event Receiver using the Visual Studio Extensions for SharePoint Services.

Software Requirements

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

Visual Studio 2005: Download page
Visual Studio 2008: Download page
Preconfigured Virtual Machine: Download page

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.

Now add a new item to the project called Event Receiver (Project -> Add new Item -> Event Receiver)

You will be asked to what type of list your event receiver should be assigned to:

Pick document library.

Now your project should look like this:

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.

  1. /// <summary>
  2. /// Synchronous before event that occurs before an existing item is completely deleted.
  3. /// </summary>
  4. /// <param name="properties">
  5. /// A Microsoft.SharePoint.SPItemEventProperties object that represents properties of the event handler.
  6. /// </param>
  7. public override void ItemDeleting(SPItemEventProperties properties)
  8. {
  9.   properties.Cancel = true;
  10.   properties.ErrorMessage = "Deleting of Items not allowed!";
  11. }

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.

What happen’s:
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:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Feature
  3.   Id="ddeefbb1-cff7-4198-9170-f1a477168e5e"
  4.   Title="File keeper"
  5.   Description="event receiver that prevents doclib files from being deleted."
  6.   Scope="Web"
  7.   Version=""
  8.   Hidden="FALSE"
  9.   DefaultResourceFile="core"
  10.   xmlns="">
  11.   <ElementManifests>
  12.     <ElementManifest Location="MyEventReceiverItemEventReceiver\ItemEventReceiver.xml" />
  13.   </ElementManifests>
  14. </Feature>

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:

EventReceiver-Installer for SharePoint

Have fun,

kick it on