Thursday, 20 March 2008

Accessing BeforeProperties and AfterProperties on List Item Event Handler

I have had numerous occasions during event handler development where i have needed to access the BeforeProperties and AfterProperties of a list item which is being used to compare or retrieve values. 

You can attack this by either accessing the property directly or looping through the HashTable collection of properties using the Key/Value pairing.  The key thing to note here is that it is solely meta data you are returning, and there is no opportunity to cast to an SPField type.

Accessing Property Directly

Code Snippet
  1. string valueOfBeforeField = properties.BeforeProperties["fieldname"].ToString();
  2. string valueOfAfterField = properties.AfterProperties["fieldname"].ToString();

Looping through property Collection

Code Snippet
  1. foreach(DictionaryEntry de in properties.AfterProperties)
  2. {
  3.     if(de.Key.ToString() == "fieldname")
  4.     {
  5.         string valueOfField1 = de.Value.ToString();
  6.     }
  7. }

0 comments:

Post a Comment