Those that are familiar with event handlers on Document Library lists in WSS v2 will be excited to know that event handlers can now be attached to any type of list. Not only do we get access to the Asynchronous events such as ItemAdded, we now also get access to Synchronous event before the fact, like ItemAdding. This will allow greater control over the way in which lists can be used within WSS v3.
This article will set out to describe the step by step instructions for defining an event handler and then hooking it up to a WSS list.
Creating the event handler
Creating an event handler is extremely simple, use Visual Studio .NET 2005.
1. Add a reference to the Microsoft.SharePoint assembly.
2. Create a class that inherits from one of the new event receiver classes, like SPItemEventReceiver or SPListEventReceiver.
3. Implement an override method for the event you wish to implement. Something like the following;
public class ListEventHandler : SPItemEventReceiver
public override void ItemAdded(SPItemEventProperties properties)
SPListItem listItem = properties.ListItem;
listItem["ColumnName"] = "Hello";
public override void ItemDeleting(SPItemEventProperties properties)
properties.Cancel = true;
properties.ErrorMessage = "Deleting items from the list is not permitted.";
The ItemAdded event will set a column in the list called “ColumnName” to a value of ‘Hello’ and the ItemDeleting event will be cancelled with an error message displayed to the user.
4. The next step is to sign the assembly and deploy it to the GAC.
That is it; we have created our event handler and deployed it ready to a list to start consuming it. The next step is to register the event to be fired from a given list.
Registering the event handler with the list
In the previous version of WSS you could register the event handler using the SharePoint user interface. This facility has been removed in WSS v3. Registering the event handler in WSS v3 can be done in two ways, as a feature or via code. I will describe the registering via code method. For more information on creating a feature the article called “Working with Features” in the WSS SDK documentation.
The easiest way to register the event handlers against the list is to create a Console application, like the one below;
public class Program
static void Main(string args)
SPSite collection = new SPSite("http://server/site/");
SPWeb site = collection.OpenWeb();
SPList list = site.Lists["MyList"];
string asmName = "MyEventHandlers, Version=126.96.36.199, Culture=neutral, PublicKeyToken=f90218d0785d9063";
string className = "MyEventHandlers.ListEventHandler";
// Register the events with the list
list.EventReceivers.Add(SPEventReceiverType.ItemAdded, asmName, className);
list.EventReceivers.Add(SPEventReceiverType.ItemDeleting, asmName, className);
// Clean up the code
// Return to calling environment : Success
Basically, the code attaches to the SharePoint site and locates the list “MyList” then adds an item to the EventRecievers collection of the list for each event to be fired.
As you can see it is pretty easy to deal with the new event handlers in WSS v3. Happy coding.