Contact Us
Please fill out the form below and we will get in touch within four business hours.

*
*
*
*
*
*
*Interested In:
(Check all that apply)



 
 
Synergy Corporate Technologies
Synergyonline > Blog > Posts > Working with BeforeProperties and AfterProperties on SPItemEventReceiver
September 18

Working with BeforeProperties and AfterProperties on SPItemEventReceiver

As many of you know, event receivers are a great way to hook into various SharePoint events.  These can apply to Feature events such as FeatureActivated, List events such as FieldAdded, and many others.  The most common set of receivers used, however, are part of SPItemEventReceiver which let you wire your code up to a number of events that can occur to items on a list or library.

When working with events, you’ll quickly find that before (synchronous) and after (asynchronous) events exist, and the method suffix such as “ing” (e.g. ItemAdding) and “ed” (e.g. ItemAdded) will tell you whether it gets invoked before or after the actual change is made.  Basic stuff.

And, as you get deeper, you’ll even find that you can extract the before and after state of the change.  For example, you can hook into the ItemUpdating event for a document library and prevent a user from changing a certain column.  The code might look like this:

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

For a document library, this works just fine.  However, you should know that the BeforeProperties hash table is not populated for items on a list.  As is worded in the SDK: “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”

When they say “not available for post events on list items”, do they mean after events (like ItemUpdated, ItemDeleted, etc)?  The wording is curious here, so I thought I’d take some time to test each combination of common events such as Add, Update and Delete.  These were done across a custom list and then a document library.  Each test involved adding a new item, editing the item and then deleting the item.  Here are the results for a 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 same test against a 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.  My analysis yields the following results:

  • Not surprisingly, we get null values for for ItemAdding (before item is added) and ItemDeleted (after item is deleted).  This was proven by Ishai Sagi some time ago.
  • 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";
}

I hope this post gives you a better idea of how before and after events work for both lists and libraries.  Your comments and feedback are always welcomed.

Technorati:

For the latest news, tips and tricks from Synergy Team in SharePoint Development World, please check out our SharePoint Development Blog Library​.

Comments

Re: Working with BeforeProperties and AfterProperties on SPItemEventReceiver

Very clear Explanation !!
 on 10/13/2011 1:35 PM

Re: Working with BeforeProperties and AfterProperties on SPItemEventReceiver

Very informative!
Extending the table with checkin/out event would be great :)
 on 2/13/2012 6:53 AM

Perfect

No too long yet  very Informative, exactly how I like it
 on 2/24/2012 11:19 AM

What I needed

Thanks
 on 3/23/2012 4:33 AM

Nice

Greate, i was looking for the original value of the ItemUpdating event, and this has helped me as guide! thanks dud
 on 4/12/2012 10:21 AM

Thanks

You are man
 on 6/22/2012 7:13 AM

Re: Working with BeforeProperties and AfterProperties on SPItemEventReceiver

great explanation sir...
 on 7/14/2012 4:11 AM

Re: Working with BeforeProperties and AfterProperties on SPItemEventReceiver

Great Expalnation...But Iam not able to get the values in document library Before and After properties in Item Updating event.
 on 8/3/2012 4:14 AM

Thank you

Thank you!!
 on 8/9/2012 7:51 AM

Thank you for wonderful information

This article helped me a lot
 on 10/4/2012 11:09 AM
1 - 10Next

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Captcha

 

Attachments


United States
55 Greens Farms Road
Westport, CT 06880
 
1050 Bishop St.
Suite 176
Honolulu, HI 96813
United Kingdom
Unit 13 Elder Way Waterside Drive Langley Berkshire SL3 6EP
United Kingdom
Singapore
Level 37
Ocean Financial Centre
10 Collyer Quay
Singapore 049315
Asia Pacific
Level 6
115 Pitt Street
Sydney NSW 2000
Australia 
+1800-930-4771
+44 (0)1753 541 000
+(65) 6232 2329
+61 2 9113 7243
United States
United Kingdom
Singapore
Australia
This web page conforms to W3C's "Web Content Accessibility Guidelines 1.0" Level "A" © 2014 Synergy Corporate Technologies
This site is best viewed in IE8 or above. Some features may not render properly if you are using an older browser.