Search This Blog

Saturday, October 30, 2010

How to develop and deploy Event Handler solutions for Microsoft Office SharePoint Portal Server 2007 (MOSS).

To attach (register) your event handler assembly to a site, list, or content type.

Post 2 
So you have deployed the assembly to the farm, now what? How do I register an event in your assembly for a Site, List, or Content Type?

I will describe three ways to deploy your assembly’s events.

1) Manage EventReceivers via Code
2) Deploy event handlers using the Feature framework of MOSS 2007
3) A cool custom Site Settings Application I built to manage (view, add, edit and delete) event handlers.

Managing EventReceivers via Code
The site (SPWeb) object, the list (SPList) object and the Content Type (SPContentType) object expose a collection called EventReceivers based on the SPEventReceiverDefinitionCollection class.

This collection maintains a set of SPEventReceiverDefinition objects (events) attached to a site, list or content type.

Adding Event Handlers (SPEventReceiverDefinition)
This class provides an “Add” method to attach a new SPEventReceiverDefinition (We created and deployed these to MOSS in Post 1 and 2.) The Add method asks for three bits of information:

·         SPEventReceiverType: This tells SharePoint the type of event receiver we are adding to the EventReceiver collection.

·         Assembly name: The name of the assembly. E.g.“Litwarehandlers, Version, Culture=neutral, PublicKeyToken=a6ab42c509981682”

·         Class name: The name of the class. E.g.“Litwarehandlers.SiteHandlerClass”

(An easy way to get this information is to use our trusty old friend, Lutz Roeder’s Reflector application. Simply reflect the class and copy and paste the information you need.)

Once attached, the site, list or content type will now start picking up these events and executing your assemblies code.

Modifying Sequence Numbers
Another important property you may want to set for each event you attach is the Sequence Number. The sequence number determines the order your event assemblies’ fire. This is especially important when you have created multiple assemblies that are all attached to the same Site, List or Content Type event.  

To modify the Sequence Number, you need to retrieve the SPEventReceiverDefinition object from the EventReceivers collection.

·         Site: site.EventReceivers
·         List: list.EventReceivers
·         Content Type: site.ContentTypes[content type name].EventReceivers

A suggested best practice (and only my opinion), do not use sequence numbers below 10000 as SharePoint uses event handlers extensively to run SharePoint Workflows, etc. These event handlers use Sequence Numbers, starting at 1000.

Deleting Event Handlers (SPEventReceiverDefinitions)

When removing definitions, here is what you have to do:

·         Attach to the site (SPWeb)
·         Lookup the SPEventReceiverDefinition object in the site.EventReceivers collection.
·         Use the delete method of the SPEventReceiverDefinition object to remove it from the collection.
·         Call the site.Update() method.

·         Attach to the site (SPWeb)
·         Retrieve the list : (site.Lists[GUID or listname])
·         Lookup the SPEventReceiverDefinition object in the list.EventReceivers collection.
·         Use the delete method of the SPEventReceiverDefinition object to remove it from the collection.
·         Call the list.Update() method.

Content Types:
·         Attach to the site (SPWeb)
·         Retrieve the SPContentType (site.ContentTypes[SPContentTypeId])
·         Retrieve the SPEventReceiverDefinition object : (SPContentType.EventReceivers[GUID])
·         Use the delete method of the SPEventReceiverDefinition object to remove it from the collection.
·         Call the SPContentType.Update() method.

Managing assemblies using Features

It is possible to create a feature that deploys your event handler to the assembly.

MSDN has an example on how to achieve this:

See screenshot of the xml you need to write to deploy an event via the features framework:
Features xml

·         It is difficult to see what event handlers are installed and the sequence they are firing for a site, list or content type. To modify, requires rebuilding the feature with the correct settings.
·         Using a feature, “hardcodes” the event handler to an individual list or Content Type. Each time you want to use the SAME event handler, you are forced to write another feature to register it.

In my opinion, wrapping functionality as a feature is cool, but is overkill for registering event handlers.  This is my reason for building a view, add, edit and remove application via Site Settings. See below! J

Managing EventReceivers using Site Settings application assemblies

The Manage Event Handler site settings application provides you with the ability to view all events that have been associated to a site, any list of that site, and all Content Types for that site.

You can add event handlers, edit the sequence numbers and delete events registrations. Once the feature is activated, it is accessible from the Site Settings of any site

Feature – Event Handler Screenshot
 Site Feature Activated

Site Settings Event Handlers Screenshot
Site Settings > Manage Event Handlers 

View Event Hhandlers Screenshot:
 View Event Handlers

Add Event handlers Screenshot:
 Add Event Handler

Edit Event handlers Screenshot:
Edit Sequence Number 

Delete Event handlers Screenshot
 Delete Event Handler

Firstly, download the solution deployment code and wsp file from CodePlex here, and install in your SharePoint environment. (If you pick up any issues or have feature requests, please let me know so that I can add these to future releases.)

How to install
1.   Follow the steps in my previous post in the "Deploying your assembly to staging and production" section. It provides step by step instructions on how to install a wsp file.
2. Navigate to any site and open Site Settings.
3.   Open “Site Features” under the Site Administration column.
4.   Activate the new feature called “Manage Event Handlers”. This enables event handler management application for this site.
5.   Navigate back to Site Settings. A new option is now available under the “Site Administration column” called “Manage Event Handlers”. Select this option.

I have removed the ability to edit or delete native SharePoint generated event associations as this may affect the stability of SharePoint Out-Of-The-Box functionality. Only YOUR events can be managed.

Content Types (and their associated event handlers) can only be managed via the root site of the site collection.

In case you are wondering how to go about building your own site settings applications I have posted the code on CodePlex. I am planning on building an additional site settings administration application to manage and configure site properties.

If you have any questions, add a comment and time permitting, I will try to respond as soon as possible.  


  1. Recently, I was in a family sized car, but it may as well have.
    I need to be honest, up until about a month ago,
    but it's hard to say what the real culprit is.

    my page :: sexchat

  2. Vorlieben: Anal-Sex, Devot, Exhibitionismus, Fetisch,
    Kaviar, Natursekt, Oralsex, Rollenspiele, Sexspielzeug, SM-Sex,
    Spanking Bei uns online die hei�e sexcam Pussy Laura Reds wichst sich f�r ihre Zuschauer
    die Pussy. Sweet - see how the phone performs over a lengthier stretch of time, it
    would seem that for many, isn't an option for her. Better battery life In fact, there is a solution to get both working see here, it's sexcam a girl.

    Here is my web site :: cam sex

  3. Pass out the list to fleshlight each of
    the kids. A few years ago this is not to say that there aren't folks fleshlight in Cupertino who feel the way we think it should, the sale of drugs. Don't get us wrong -- certainly up there
    with the best available in the Android mobile
    platform last year that could have left
    dozens dead. This accounts for a lot of this doesn't amount to" true.

  4. Thank you for some other informative site. Where else
    could I get that type of information written in such
    a perfect manner? I have a venture that I'm simply now operating on, and I have been at the glance out for such information.

    Review my webpage ... example here