Getting started with custom SharePoint Event Receivers
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
Description
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.
-
/// <summary>
-
/// Synchronous before event that occurs before an existing item is completely deleted.
-
/// </summary>
-
/// <param name="properties">
-
/// A Microsoft.SharePoint.SPItemEventProperties object that represents properties of the event handler.
-
/// </param>
-
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.
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:
-
<?xml version="1.0" encoding="utf-8"?>
-
<Feature
-
Id="ddeefbb1-cff7-4198-9170-f1a477168e5e"
-
Title="File keeper"
-
Description="event receiver that prevents doclib files from being deleted."
-
Scope="Web"
-
Version="1.0.0.0"
-
Hidden="FALSE"
-
DefaultResourceFile="core"
-
xmlns="http://schemas.microsoft.com/sharepoint/">
-
<ElementManifests>
-
<ElementManifest Location="MyEventReceiverItemEventReceiver\ItemEventReceiver.xml" />
-
</ElementManifests>
-
</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,
Alex





[...] this post Alex describes how to create a custom SharePoint Event Receiver using the Visual Studio Extensions for SharePoint [...]
Pingback on July 27, 2008 @ 11:15:19
How can we display the error message on error page when using a custom master page. I got it working but the message is displayed like unhandled exception which looks very crepy.
Thanks
Comment on November 3, 2008 @ 17:12:29
Excellent introduction. These work great in Standard view. I’ve been looking for a way to give good error messages while editing in Datasheet view. No luck so far.
Comment on November 15, 2008 @ 02:55:31
Very good demontstration about Event Receivers.
Comment on November 26, 2008 @ 11:23:07
Nice overview! Thank you…
Comment on January 22, 2009 @ 09:08:34
Very good article! Some people may want to do it on their own meaning without Visual Studio Extensions for SharePoint Services. Which is easy and does not require much. Feel free to check example at Malcan
Comment on February 10, 2009 @ 19:09:27
Hi Nafees
For showing the error essage in your custom master page, You have to change master page reference of error.aspx inside 12\layouts\Template\.
Samarendra Swain
Mindfire solutions
Comment on February 13, 2009 @ 08:46:21
Прикольно! Все бы так писали
Comment on May 28, 2009 @ 02:57:36
Спасибо
Comment on May 28, 2009 @ 08:17:20
Great & simple post!
Comment on August 17, 2009 @ 16:10:19
I looking for event reciever for field not for item
Comment on September 14, 2009 @ 11:06:50
Try this, I found something like this one
working with sharepoint list
http://sarangasl.blogspot.com/2009/09/in-this-article-im-going-to-describe.html
Comment on September 29, 2009 @ 14:12:09
Hi Alexander,
I followed your steps very clearly and to my surprise it was good to know that this method can be used to avoid all the Feature adding, all GAC deployment and all the rest of it.
However, I tried using your steps on ‘Custom List’. I created a new custom list and added a new item and then tried to delete it. To my surprise, the item deleted and no error messages were shown.
Can you possibly point out why. Is there something that needs to be set up from Central ADministration or is it because of something else.
Please reply
Comment on November 9, 2009 @ 18:21:24
Hi Alexander,
Many Million thanks for this article and it saved me so much time. Now, my event handler is working.
I made a terrible mistake of adding code into ‘ItemDeleted’ event instead of ‘ItemDeleting’. Then deactivated my feature and activated it back again and ‘Voila’ it works.
Cheers…!
Comment on November 9, 2009 @ 19:31:41
Alex,
Thanks for sharing this using steps and it is very clear and helpful. You mentioned if I want to enable this feature in a list or library in a subsite, we need to use the custom Feature Event Receiver. Could you elaborate a little more on this where I can find this event receiver in Visual Studio? By the way, I have Visual Studio 2010 installed. Does it work for SharePoint 2010?
Please advise.
Brian.
Comment on November 4, 2012 @ 18:17:09
Alex,
You mentioned Preconfigured Virtual Machine download page on this document. Couly you point me to the exact link? Above link point me to the Microsoft download page only.I need to download a virtual machine and try your method.
Thanks,
Brian.
Comment on November 4, 2012 @ 18:45:10