LINQ Extension Methods and Lambda Expressions in SharePoint 2007 Development

by Oscar 27. September 2008 08:40

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).

image

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.

image 

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.


image

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.
image

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.

image

Hope this helps,
Oscar

Tags:

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About Oscar

Oscar Medina - SharePoint Solutions Architect

I am a SharePoint Solutions Architect and Managing Partner at SharePointAce Consulting Group, LLC.  I have over 13 years of software development experience. 

Previously a Consultant at Microsoft Consulting Services (MCS) North West Region with focus on the SharePoint Products and Technologies Platform.  I have been involved with SharePoint since it's first version (SharePoint 2001) and have grown more gray hair providing solutions ever since :)

Author of the CQWP – Recurring Calendar Events at http://cqwprce.codeplex.com

 

Follow SharePointAce on Twitter! Follow me on Twitter.


Contributor to the book:

 

Occasional Speaker at SharePoint Saturday - SharePoint Saturday

RecentComments

Comment RSS