CJ

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

  • Past Post

  • Vistor Locations

Registering SharePoint Event Handlers

Posted by Clayton James 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.

Advertisements

8 Responses to “Registering SharePoint Event Handlers”

  1. Chris said

    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.

  2. Jazz said

    Thanks. This helpd!!

  3. Hi Chris,

    I’ll give you a latter answer.

    VSeWSS generate package wich contain Xml activates features.

    So, I prefer write my own files : ddf, manifest, feature.xml,etc.

    And I also write my command file : deploy.cmd, install.cmd. Then, you activate your features using stsadm option.

    And you’re the master of deployment.

    Cheers !

  4. mohsin said

    hi,

    i have a news site (variation implemented). i want to capture page update event as i have to send the same content with some addtions to different email addresses when the page is published.

    the problem is that i am not able to capture event any event of those news pages while able to capture events of other lists. i have set ListTemplateId=”101″.

    any clue

  5. creeduckk said

    hello

  6. TechnoBtye said

    How are you all doing?

  7. RobertPl said

    top seo software to rank on google

  8. Florine said

    With this unique approach you can now get your company certified first time

    With a 10 year of track-record assistance, Clients successfully accomplish their certification standards in a mean time of 45 days…. Compliance favors to get you certified in minimum time and less amount of fuss!!

    KAYZED Consultants: Assuring best of Quality Management System, Quality Assurance Services, and Food Safety Systems

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: