views:

192

answers:

2

I'm writing an event handler to handle the updating of a particular SPItem in a list. The event is asynchronous and I get the SPEvenItemProperties without a problem. What I would like to find out is which of the SPItems columns' actually fired the event. Anyone have any idea how?

Thanks in advance.

+3  A: 

I think the best way to do this would be to look through the BeforeProperties and AfterProperties of the SPItemEventProperties and check which fields have different values.

These contain the values of all fields of the item before and after the event occurred.

Simon Fox
Actually, that won't work unless it is a document library with versioning turned on. Regular lists, with or without versioning, will not populate the BeforeProperties at all, and all columns will be empty.
Bjørn Furuknap
So instead you may have to create two separate handlers, one for ItemUpdating (where you would somehow store the current set of property values) and one for ItemUpdated (where you would get the new set of property values and then compare with those that were saved by the Updating handler)...
Simon Fox
+1  A: 

Hello,

Your answer depends a bit on from where and how the SPListItem is retrieved. In a regular list, you do not have access to the previous values of the item. If you turn on versioning, you can get access to the previous versions, depending on permissions, of course.

For a document library you can use the SPItemEventProperties.BeforeProperties to get the previous metadata for a document.

For a document library you can try something like this:

foreach (DictionaryEntry key in properties.BeforeProperties)
{
    string beforeKey = (string)key.Key;
    string beforeValue = key.Value.ToString();

    string afterValue = "";
    if (properties.AfterProperties[beforeKey] != null)
    {
        afterValue = properties.AfterProperties[beforeKey].ToString();
        if (afterValue != beforeValue)
        {
            // Changed...
        }
    }
}

.b

Bjørn Furuknap