Thursday, February 23, 2012

Comparing Fields to See if Values Have Changed in the ItemUpdating Method

You cannot get information about what was before the event in the '-ed' EventReceiver, only in the '-ing'.

So, within the public override void ItemUpdating(SPItemEventProperties properties) method...

- The "before" updating value is obtained like this;
var beforeValue = properties.ListItem["Column Name"].ToString();

- The "after" updating value is obtained like this;
var afterValue = properties.AfterProperties[properties.ListItem.Fields["Column Name"].InternalName];

Before making a beforeValue == afterValue comparison you may need to type check these values.

Changing the "Created By" Value in a SharePoint List Item

C#


// Where "item" is of type SPItem
item["Created By"] = web.EnsureUser("AD account name").ID;
item.UpdateOverwriteVersion();