Skip to main content

Thinking out aloud - Dave Hunter's SharePoint Blog

Go Search
Home
Blog
  

Locations of visitors to this page


 Useful Links

  Microsoft UK events
  SharePoint 2007 on CodePlex
  SharePoint Community Portal
  SharePoint User Group
  SharePoint Community Portal
  SharePoint Pedia
  SharePoint MSDN Forums
  SharePoint University
Home > Thinking out aloud - Dave Hunter's SharePoint Blog > Posts > Delving into SharePoint 2007 Event Receivers
My thoughts and findings on Microsoft Information worker technologies, including MCMS and SharePoint 2007 (MOSS).
Delving into SharePoint 2007 Event Receivers
 
What are they?
 
SharePoint 2007 Event Receivers are a type of feature which listens for certain events on a site, list or list item.  Event Receivers are the MOSS version of document event handlers (also known as Event Sink) in SharePoint V2 technologies (WSS and SPS).  Previously the document event handlers were only available for document libaries.  Event receivers are now available for any type of list. 
 
What can they be used for?
 
SharePoint 2007 Event Receivers add behaviours to lists or list items.  They are commonly used to perform data validation (beyond SharePoint standard capabilities), to ensure data integrity or to perform a business process.
 
Definition
 
Event Receivers have before and after events.
 
The events relating to a site are:
  • Before
    • SiteDeleting
    • WebDeleting
    • WebMoving
  • After
    • SiteDeleted
    • WebDeleted
    • WebMoved
A class needs to inherit from SPWebEventReceiver.  The behaviour is defined by overriding a method for one of the above before or after events.  You can access properties that relates to the event that the feature is listening using SPWebEventProperties.
 
The events relating to a list are:
  • Before
    • FieldAdding
    • FieldUpdating
    • FieldDeleting
  • After
    • FieldAdded
    • FieldUpdated
    • FieldDeleted

A class needs to inherit from SPListEventReceiver.  You define the behaviour by overriding a method for one of the above before or after events.  You can access the event properties using the parameter SPListEventProperties.

The events releating to list items are:

  • Before
    • ItemAdding
    • ItemUpdating
    • ItemDeleting
  • After
    • ItemAdded
    • ItemUpdated
    • ItemDeleted
    • ItemAttachmentAdded
    • ItemAttachmentDeleted
    • ItemCheckedIn
    • ItemCheckedOut
    • ItemFileConverted
    • ItemFileMoved
    • ItemUncheckedOut

A class needs to inherit from SPItemEventReceiver.  You then override a method for one of the above before or after events.  The event properties can be accessed by the local parameter SPItemEventProperties.  One of these is the list item.  For example:

public override void ItemAdded(SPItemEventProperties properties)
{
  SPListItem currentListItem = properties.ListItem;
  // other implementation here .....
}

Other important information of the properties object is the AfterProperties and BeforeProperties.  AfterProperties provide access to the modified properties.  BeforeProperties provide access to the column data before it was changed.  This enables you to perform changed based validation.

How are they assigned to a list?

Event Receivers can be bound by using the ListTemplateId specified in the feature or added to the EventReceivers Collection of a list.  If you are using the latter this is usually performed using a SPFeatureReceiver.  A Feature Receiver allows some custom code to be executed once the feature has been installed, activated, deactivating and uninstalling (note the first two are after events and the last two are before events).  The code for adding an Event Receiver to a list is:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWeb web = properties.Feature.Parent as SPWeb;
    web.Lists["Rating History"].EventReceivers.Add(
SPEventReceiverType.ItemAdded,
"Dhunter.SharePoint.Eventhandlers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0a26195ff03df1fd",
"Dhunter.SharePoint.Eventhandlers.RatingSummaryEventHandler"); }

As you can see above, the Add method takes in the 4 part string that correctly identifies the assembly and class for the event handler.

How are events registered with the event receiver?

Events that the Event Receiver responds to are subscribed to using the feature file.  For example:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="100">
    <Receiver>
      <Name>RatingSummaryEventHandler</Name>
      <Type>ItemAdded</Type>
      <SequenceNumber>10000</SequenceNumber>
      <Assembly>Dhunter.SharePoint.Eventhandlers, Version=1.0.0.0, Culture=neutral, 
PublicKeyToken=0a26195ff03df1fd</Assembly> <Class>Dhunter.SharePoint.Eventhandlers.RatingSummaryEventHandler</Class> <Data></Data> <Filter></Filter> </Receiver> </Receivers> </Elements>

Or if the event receiver is bound to the list by code, you specify this when adding the event handler using the enumeration SPEventReceiverType.  You add the event receiver to the EventReceivers collection once for every SPEventReciverType.

For example the code below adds the event handler for the ItemAdded and ItemUpdated events.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWeb web = properties.Feature.Parent as SPWeb;
    web.Lists["Rating History"].EventReceivers.Add(
SPEventReceiverType.ItemAdded,
"Dhunter.SharePoint.Eventhandlers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0a26195ff03df1fd",
"Dhunter.SharePoint.Eventhandlers.RatingSummaryEventHandler"); web.Lists["Rating History"].EventReceivers.Add(
SPEventReceiverType.ItemUpdated,
"Dhunter.SharePoint.Eventhandlers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0a26195ff03df1fd",
"Dhunter.SharePoint.Eventhandlers.RatingSummaryEventHandler"); }

 

Using Before Events

It's possible to cancel the task executed using a before event.  You may need to implement this behaviour because you dont want users to modify a schema of a list or that certain criteria failed when a new item has been entered.  For example:

properties.ErrorMessage = "Only Administrators are allowed to modify the definiton of this list";
properties.Cancel = true;

Before events are often referred to as synchronous events.  Synchronous events stop the current flow of execution until the code processed in the method for the event handler has run.  These are typically used to perform validation on the entered data to maintain data integrity or provide custom validation rules that the front-end cant provide.

Using After Events

After events are asynchronous events.  This doesn't stop the execution of the current flow.  These are typically used to kick off a business process.

How are they bound to a content type?

In the content type definition file you need to add a XmlDocuments element that contains a XMLDocument followed by spe:Receivers and Receiver elements.  For example:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <ContentType ID="CONTENT TYPE ID" Name="CONTENT TYPE NAME" Group="Custom" 
Description="CONTENT TYPE DESCRIPTION" Version="0"> <FieldRefs> <FieldRef ID="FIELD ID" Name="FIELD NAME" /> </FieldRefs> <XmlDocuments> <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/events"> <spe:Receivers xmlns:spe="http://schemas.microsoft.com/sharepoint/events"> <Receiver> <Name>RatingSummaryEventHandler</Name> <Type>ItemAdded</Type> <SequenceNumber>10000</SequenceNumber> <Assembly>Dhunter.SharePoint.Eventhandlers, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=0a26195ff03df1fd</Assembly> <Class>Dhunter.SharePoint.Eventhandlers.RatingSummaryEventHandler</Class> <Data></Data> <Filter></Filter> </Receiver> <Receiver> <Name>RatingSummaryEventHandler</Name> <Type>ItemUpdated</Type> <SequenceNumber>10000</SequenceNumber> <Assembly>Dhunter.SharePoint.Eventhandlers, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=0a26195ff03df1fd</Assembly> <Class>Dhunter.SharePoint.Eventhandlers.RatingSummaryEventHandler</Class> <Data></Data> <Filter></Filter> </Receiver> </spe:Receivers> </XmlDocument> </XmlDocuments>
</ContentType> </Elements>

What sequence numbers?

Sequence numbers apply the order that the event handlers should be executed.  Take this example ... You have a list which has multiple event handlers bound (Event handler 1 and Event handler 2).  Both event handlers are registered to the ItemAdded event.  Event handler 1 has a sequence number of 900 and Event handler 2 has a sequence number of 1000.  This means Event handler 1 will be executed first and then followed by Event handler 2.

Deployment considerations

The compiled class file must be strongly named and deployed to the GAC (Global Assembly Cache).

If you bind the event receiver using a feature file it can only be scoped at the site level.  It cannot be bound to an instance of a list or to a content type.

Points to note

Disable Event Firing. 

If you modify the current SPList for a List Event Receiver or SPListItem for an Item Event Receiver you must call DisableEventFiring() before commiting the change.  This stops recursive event firing and the possiblity of entering an infinite loop.  After you have committed the change, you should then call EnableEventFiring() to return the processing to its normal behaviour.

Performance

What effect would bulk creation or upload of items have on the perfromance of our system?  You should bear this in mind when designing, developing and testing the system.  Intensive calls during event handlers will hinder the performance of the system, especially with synchronous events as the flow of execution is stopped until the code within the event handler is executed.

  Copyright
This work is licenced under a Creative Commons Attribution-Noncommercial-No Derivative Works 2.0 UK: England & Wales License
  Site Map

Comments

MSDN article - Troubleshooting Event Handlers

Please see http://msdn.microsoft.com/en-us/library/aa979520.aspx for a MSDN article on Troubleshooting Event Handlers.
Dave Hunter at 02/06/2008 16:56

thx

Nice overview! Thank you...
at 02/07/2008 08:48

Uninstalling an even reciever Special case

I have deployed an event reciever that attaches to a list type, so all form libraries, have an item added event receiver.

I have unistalled the feature, and expected the all the event receivers to be deactivated, but no go!! do I have to go through each forms list and make take the feature out? how does that effect new Form Lists?? will they have the feature in there??

at 07/08/2008 21:15

nice article

nice article
at 22/10/2008 06:48

event handlers and workflow

I have a workflow that I want to start after a user uploads a document and enters the metadata, but am having difficulty picking the right spot to start it.  If I just start the workflow on the new file, it starts before all the metadata is added.  I thought about starting in a custom list event receiver, but it is not obvious to me which event to use.  Any ideas?  thanks
at 22/10/2008 23:26

Content Types

I have a content type that I defined through the Site Collection Site Settings UI (i.e. no content type definition xml file).  How do I associate my Feature only with those type of Content Types?

Thanks for any help.
at 02/12/2008 17:06

Event Receiver ok for administrator but not for contributor

Hi (excuse for my English)

I have a class eventhandler (MonEventHandler.cs) which implement de SPListEventReceiver which permit to managed added and deleted document in a document library (ItemAdded et ItemDeleted). I have in my code deployed my class
 
string docLibName = "docLibToto";
string assemblyName  = "MyEventHandler";
string publicKeyToken = "xxxxxxxxxxx";
string AssemblySignature = assemblyName + ", Version=1.0.0.0, Culture=neutral, PublicKeyToken=" + publicKeyToken;
string className = "MyEventHandler.EventReceiverDocumentLibrary";
 
aWebSite.Lists[docLibName].EventReceivers.Add(SPEventReceiverType.ItemAdded, AssemblySignature, className);
 
These it's ok when a system account upload a document. But when a contributors do the same action it's not ok. My class which managed event receiver is not used (I konw because my first line in ItemAdded  method is a log "Start ItemAdded"  which is logged for system account but not for a contributor).

Do you know what I must do ? Thanks

Mathieu.
at 29/01/2009 17:33

Too Good

Thank you very much for the overview. Excellent!! Keep up the ggod work.
at 08/07/2009 13:09

Great article

This is one the most well written, comprehensive article on anything to do with SharePoint lists I've read!!! Thanks!
at 28/07/2009 14:35

 Dave Hunter

I'm currently a Senior Consultant at Netstore 2e2. I specialise in Microsoft Information Worker technologies (especially MOSS, MCMS and .NET), with over 8 years of experience within this area of specialism. 

I have gained two certifications for MOSS and WSS and look to complete the full certification track soon.

View Dave Hunter's profile on LinkedIn

  Subscribe in a reader


 Latest Posts

I’ve been Awarded a MVP for SharePoint01/04/2010 19:49
SharePoint 2010 Training on Microsoft E-Learning15/03/2010 17:56
SharePoint UK User Group - Thursday 27th August London Meeting25/08/2009 17:56
CAML Query that filters on the current user23/07/2009 17:50
Microsoft Ramp Up Free SharePoint Developer Training22/07/2009 20:59
How To: Change a SharePoint Application Pool Programmatically07/07/2009 18:08
SharePoint Forums Topping 100 Answers06/07/2009 12:29
Find out the SharePoint Internal Name for a Column or Site Column28/05/2009 09:56
Delving into Deploying SharePoint Artifacts with Features24/04/2009 15:20
SharePoint - Connecting MySites with the Portal24/04/2009 14:39
1 - 10 Next