Hip hip hooray…it’s my birthday…pretty sad that I would be blogging first thing on my birthday, but hey it’s a nerd thing to do.
It is nearly midnight on Christmas night 2006, so that means that it is almost my birthday… Happy Birthday to me! Merry Christmas to everyone and have a safe new years.
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=188.8.131.52, 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.
The MOSSIG team was able to raise $718 in donations for movember. This amount will be matched by the company I work for SDM. So a total donation of around $1450 has been raised. I think this is excellent and enjoyed growing my mo. I have however removed it as it didn’t really suit and the itchness wasn’t really growing on me.
Today I am helping run the MOSSIG and Victoria.NET hands on development day at Cliftons in Melbourne. We are running two streams. Stream 1 is on the new .NET 3.0 framework and stream 2 is on Office 2007.