How Value Return in Before Properties, After Properties and ListItem using EventReceiver in SharePoint2010

Event receivers are common in SharePoint development so its better to understand the data available in each events. Sometimes as a developer we jump into coding before thinking about contextual data availability.One more important thing to notice list event receiver  and document library event receiver are different items of contextual data availability.Following Table will give you a clear picture about the contextual data in each events.

  • SharePoint List

Event

Before Properties Returns

After Properties Returns

ListItem Returns

Event Type

Item Adding

Null Value

New Value

Null Value

Synchronous

Item Updated

Null Value

New Value

New Value

Asynchronous

Item Updating

Null Value

Changed Value

Current Value

Synchronous

Item Updated

Null Value

Changed Value

New Value

Asynchronous

Item Deleting

Null Value

Null Value

Current Value

Synchronous

Item Deleted

Null Value

Null Value

Null Value

Asynchronous

  • SharePoint Document Library

Event

Before Properties Returns

After Properties Returns

ListItem Returns

Event Type

Item Adding

Null Value

Null Value

Null Value

Synchronous

Item Updated

Null Value

Null Value

New Value

Asynchronous

Item Updating

Current Value

Changed Value

Current Value

Synchronous

Item Updated

Current Value

Changed Value

New Value

Asynchronous

Item Deleting

Null Value

Null Value

Current Value

Synchronous

Item Deleted

Null Value

Null Value

Null Value

Asynchronous

Synchronous and Asynchronous Event Handlers

In SharePoint 2010 it is possible to handle events that occur on list items. These types of events come in two types: Synchronous and Asynchronous Event.

  • Synchronous: happens ‘before’ the actual event, you have the HttpContext and you can show an error message in the browser and cancel the event
  • Asynchronous: happens ‘after’ the actual event, there’s no HttpContext  and you cannot directly show an error message or cancel the event

By sub-classing Microsoft.SharePoint.SPItemEventReceiver you can override the desired “event” methods.

  • Synchronous: methods ending with ‘-ing’ (ItemAdding, ItemUpdating, …)
  • Asynchronous: methods ending with ‘-ed’ (ItemAdded, ItemUpdated, …)

If you implement an override method you might want to place your code between the following lines to avoid raising other events unwantedly (and possibly causing an infinite loop).

this.DisableEventFiring(); 
... 
this.EnableEventFiring();

Each override method has a parameter of type Microsoft.SharePoint.SPItemEventProperties that contains the item and event properties available for use. There’s a bug in the SPItemEventProperties.AfterProperties dictionary that appears empty for synchronous events but it isn’t. You can iterate over the dictionary to read and write to this collection ! This means that you can modify some of the inputted metadata before the actual item is stored in the list.

The thing to remember here is that the dictionary uses the field internal name as key, so field ‘Field’ could have an internal name such as ‘My_x0020_custom_x0020_field’.