BeforeProperties and AfterProperties in Sharepoint Event Receivers

Many of us have used BeforeProperties and AfterProperties while working with Event Receiver, but we don’t know how and when to use these properties while accessing the data/field value.

These are facts we need to consider while working with Event receiver.

1) We should not use Before properties for accessing change of value at List level.
2) Before properties are available only with Document Library

I am summarizing about BeforeProperties, AfterProperties and properties.ListItem in sharepoint event receiver context.

List:

List BeforeProperties AfterProperties properties.ListItem
ItemAdding No value New value Null
ItemAdded No value New value New value
ItemUpdating No value Changed value Original value
ItemUpdated No value Changed value Changed value
ItemDeleting No value No value Original value
ItemDeleted No value No value Null

Library:

Library BeforeProperties AfterProperties properties.ListItem
ItemAdding No value No value Null
ItemAdded No value No value New value
ItemUpdating Original value Changed value Original value
ItemUpdated Original value Changed value Changed value
ItemDeleting No value No value Original value
ItemDeleted No value No value Null

Here one more scenario we need to consider before we conclude this topic.

I have one example where I need to prevent the user from entering the same value for a column.

Ex: Suppose column called Description and its initial value is “Test” and user go to the list and update the item for that column with the same value we need to prevent this. How to achieve this?

So if we observe carefully with above table then we have answer available immediately.
In the ItemUpdating Event we can check the condition like

if(properties.ListItem[“Description”] == properties.AfterProperties[“Description”])
{
//Cancel the event
properties.Cancel = true;
properties.ErrorMessage = “The column is having same original value”;
}

Update()

  • Updates the item in the database
  • Updates the “Modified” and ?”Modified by” values
  • Creates a new version

SystemUpdate()

  • Updates the item in the database
  • No changes in the “Modified” and “Modified By” fields
  • No new version
  • Triggers the item events

SystemUpdate(true)

  • Same as SystemUpdate() and increments the item version
  • Using SystemUpdate(false) is exactly the same as SystemUpdate()

UpdateOverwriteVersion()

  • Updates the item but does not create a new version
  • Updates the “Modified” and “Modified by” values

source: http://srisharepointdevelopment.blogspot.de/2013/06/beforeproperties-and-afterproperties-in.html