Handling SharePoint Events

Event Handling in SharePoint requires the creation of an event receiver class that inherits from a SharePoint base receiver class. In this class you over-ride the event methods you want to receive. As I like to look at it, there are 2 kinds of events: completing and completed events. Completing cover events that are in-progress: adding, updating deleting. These events capture an action before data is written to the content database. In these events you can access the SharePoint object’s current properties (properties.ListItem[“column”]) as well as the new properties that will be committed when the event is completed (properties.AfterProperties[“column”]). There is an exception though- adding events will not allow current properties to be accessed because the object does not exist within the database until this event is completed. Only the AfterProperties are available in this case.

Completed cover events that have just occurred: added, updated, deleted. When these events are fired, data has been committed to the content database. The new properties can be accessed by querying the SharePoint object’s current properties (properties.ListItem[“column”]) and the overwritten properties by accessing the object’s BeforePropreties (properties.BeforeProperties[“column”]).

Here is a simple example of using the SPItem ItemUpdated event to set a field value in the list item which has triggered the event. Notice that event firing is disabled before calling Update on the item to prevent an “infinite loop” scenario.

public class TriggerHandler : SPItemEventReceiver
{ ..

public override void ItemUpdated(SPItemEventProperties properties)
                properties.ListItem[“column name”] = ”new value”;
            catch (Exception ex)
                logStatus(“An error occurred in processing the ItemUpdated event” + ex.Message);

Once an event receiver class is deployed, it must be associated with the site, list or list item that will trigger the overridden events. There are many ways of doing this. Programmatically you can create a feature which references the Entity Template Id.

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="100">
<Assembly>Projects.Events.UpdateEventHandler, Version=, Culture=neutral, PublicKeyToken=************
<Class> Projects.Events.UpdateEventHandler. ListUpdatedEventHandler</Class>

Manually you can make use of pre-packaged utilities such as Event Handler Explorer: http://www.u2u.info/SharePoint/U2U%20Community%20Tools/EventHandlerExplorer.zip or the Event Handler Manager: http://www.codeplex.com/SPSCustomAdmin/Release/ProjectReleases.aspx?ReleaseId=3024


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