manoj´ssharepointblog

SharePoint Blog

BeforeProperties and AfterProperties of SPItemEventReceiver

Leave a comment

BeforeProperties and AfterProperties: SPItemEventReceiver

There are two type of SharePoint events: Asynchronous (After) and Synchronous (Along) events.Synchronous events, e.g. are: ItemAdding, FieldUpdating, FeatureDeactivating etc… (like all those events that are having “-ing” as suffix).Asynchronous events, e.g. are: ItemAdded, FieldUpdated, FeatureActivated etc… (like all those events that are having “-ed” as suffix).

As far as AfterProperties and BeforeProperties are concerned for any event, they can help you to extract After and Before change in state about the object in context. This case is true and valid for any document library in context. For example, if you want to prevent any change in particular column of Document library your code must be:

public override void  ItemUpdating(SPItemEventProperties 
                                             properties)
{
     if (properties.BeforeProperties["column"] != 
                              properties.AfterProperties["column"])
    {
        properties.Cancel = true;
        properties.ErrorMessage = "This column cannot be changed";
    }
}

But such examples are only valid for Document Library in context, as per the Microsoft; as it is written inSDK that “For documents, Before and After properties are guaranteed for post events, such as ItemUpdated, but Before properties are not available for post events on list items”. So, statement written like these form the basis of analysis, on testing various events during Addition, Deletion and Update  of both SharePoint lists and SharePoint library, here is the conclusion that is actually drawn for any 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
No value means that column value in the hash table was not available.
New value means that the correct value for the column was available.
Changed value means that the correct updated value was available.
Original value means that the correct original value was available.
Here is the results on any document 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
Properties.ListItem refers the the current value for the list item at that point in the event.  Null means that the item is not available.
Some noteworthy points are:
  • Not surprisingly, we get null values for for ItemAdding (before item is added) and ItemDeleted (after item is deleted).
  • As correctly documented in the SDK, item events for lists do not expose BeforeProperties.
  • ItemAdding and ItemAdded correctly report the value in the AfterProperties for an list item, but not a library item.  This is curious.
  • We have no visibility on the previous states during the ItemDeleted event.  Once it’s deleted, it’s clearly gone.

So, if we go back to our original problem listed above.  How can we prevent a user from changing a certain column for an item in a list event?  From the list table, you can see if we hook into the ItemUpdating event, we can compare the current item’s value (properties.ListItem) to the AfterProperties value.  The code would look like this:

if (properties.ListItem["column"] != 
                  properties.AfterProperties["column"])
{
    properties.Cancel = true;
    properties.ErrorMessage = "This column cannot be changed";
}

Hope this analysis provide you quite a clear picture about 
After/BeforeProperties on any Document Library and List.
Advertisements

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