CJ

The circle of life - ** .Net - Training - MOSS **

Registering SharePoint Event Handlers

Posted by CJ on April 2, 2008

The first thing you want to do if you are writing event handlers for SharePoint is download the VSeWSS 1.1. This has a whole bag of goodies for us SharePoint developers  including a nice template for creating event handlers. Unfortunately it is only available in VS2005.

There are basically two ways you can register an event handler to a SharePoint list or content type.

  1. Features - declarative approach
  2. Features and OM combined

Ok, so when would you use one instead of the other.

  1. Features
    Basically you can use the Features declarative model when you want to register an event handler to all lists of the same type (ListTemplateId). If the ListTemplateId = 101 (Document Library) as shown below then when the Feature is installed and activated then the event handler will associated to all document libraries in the site.
    Everything is declared in the ElementsManifest.xml file.
    <Elements>
      <Receivers ListTemplateId=”101″>
        <Receiver>
          <Name>SimpleEvent</Name>
          <Type>ItemAdded</Type>
          <SequenceNumber>10000</SequenceNumber>
          <Assembly>SimpleEventHandler, Version=1.0.0.0, Culture=neutral,
            PublicKeyToken=10b23036c9b36d6d</Assembly>
          <Class>CJ.Samples.SimpleEventHandler</Class>
          <Data></Data>
          <Filter></Filter>
        </Receiver>
      </Receivers>
    </Elements>Notes:
    ListTemplateId: This specifies which list to associate the event handler. 101 = document library. This is an all or nothing approach which means I can not associate the event handler to only one document library.
    ItemAdded: This is the event that will be raised when this action takes place.
    //Override the ItemAdded event and add your own code
         public override void ItemAdded(SPItemEventProperties properties)
         {
             SetSecurityForNewItem(properties.ListItem.File, properties.ListItem.ParentList);
         }
    Assembly, PublicKey Token, Class: Self explanatory. The event handler must be installed in the GAC before is can be associated to a list.
    SequenceNumber: This allows you to have multiple event handlers registered and specifiy their order of execution.
    Data
    Filter
  2. Features and Object Model
    In this approach we have the ability to register our event handler to only one (or more) list/content type. Our solution (.wsp) would manage the process of installing our event handler assembly into the GAC and activate the Feature. When the Feature is activated we can run a Feature Receiver to attach our event handler to a specified list as shown below.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWeb web = properties.Feature.Parent as SPWeb;
    web.Lists["Appointments"].EventReceivers.Add(
      SPEventReceiverType.ItemAdded,
      “CJ.SharePoint.Eventhandlers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3a29000ff02kt1bn”,
      “CJ.SharePoint.Eventhandlers.ItemPermissionsEventHandler”);
}

Side note on Event Handlers
If the event handler modifies the current list or current list item then you must call the this.DisableEventFiring() method before committing the change, and then before you leave your event handler code call this.EnableEventFiring() method to turn events back on. This stops recursive event firing from occurring where the list item is being modifed by the event handler which in turn raises the event handler again, thus causing an infinite loop.

One Response to “Registering SharePoint Event Handlers”

  1. Chris Says:

    I was reading your article and thought you may be able to answer a question i had. I have been working in VseWss 1.1 and have run into a couple of roadblocks with regards to features. Number one is how the wsp automatically activates all features for you. Is there a way to disable this? Secondly, I have found creating a feature receiver and having VseWss deploy it to be impossible. I basically created a class and input the overrides. In the feature for my list instance I referenced the feature receiver class. It is referencing correctly and actually deploys the feature however I get and “object not set to a reference…” error message. I can then go in through the UI and activate it manually. It appears that the dll is not being deployed to the gac prior to the list instance feature being activated. Any help would be appreciated.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>