I have been working on a project where I needed to do some extensive manipulation of data on a List Event Handler ItemUpdated Event. Here are a few samples of how Lambda Expressions and LINQ Extension Methods, (Scott Guthrie has a great intro article) saved the day for me.
First, I have a function which I won’t list here for now; that iterates through the afterProperties collection and checks for the fields that have changed, once the user clicked on the save or ok button. For every field that has changed, it stores the SPField, the old value and the new value in an object called CourseUpdatedField (see class below).
Retrieving all fields except one with a specific name
As I mentioned before I store the SPField in the Class CourseUpdatedField for a reason, one of those reasons is that I needed to access specific field properties, such as the InternalName, in this case to exclude that field before iterating through the Collection.
Find added and removed items on a SPFieldLookupMulti Field
For a field like this, a user can click on the ‘add >’ or ‘< Remove’ buttons. The goal is to find out what Cities where previously there, which ones where just added, and which ones where removed when the user updated the form.
The data is stored as a delimited string, so to retrieve those values, one can use the SPFIeldLookupValueCollection Class as listed below. The SPFieldLookupValue object has the City value on the LookupValue property, and this is where I compare those values to check old and new values.
And here is how I have gone about capturing these changes. When I iterate through the column that holds the new Cities, I check to see if that City existed in the old column value.
In the end, part of the List Event Handler’s job was to capture changed fields when a List Item was updated, and send an email to specific people. This is how changes to the Cities Field look in the email message.
Hope this helps,
Oscar