3
votes

I have a list which has 30 odd columns.

I have attached two event handler to the list. 1. ItemUpdating 2. ItemUpdated

In ItemUpdating event, I am checking one field value for a change.

In ItemUpdating event, I want to do the processing, if value changes. I cann't do comparision here because before properties does not provide old values in the list item.

The processing includes few jobs and sending e-mails after completion.

I am looking for a solution where I can set the bit when field value changes in ItemUpdating. Check this bit if set do the processing in ItemUpdated.

4

4 Answers

4
votes

You will not be able to share values directly, you will have to use a secondary method to persist your data.

The easiest way to do this would be to use the property bag of the list item.

//the list item you want to update (typically SPItemEventProperties.ListItem in an event receiver
SPListItem specialItem = list.Items[0];
specialItem.Properties["some_persisted_key"] = "Some Value here";
specialItem.SystemUpdate(false);

Be sure to use SystemUpdate otherwise you run into the danger of creating an endless loop (or disable event firing beforehand as described in that article).

In your ItemUpdated event you can access your value then simply by going for specialItem.Properties["some_persisted_key"].

1
votes

This is the full solution that worked for me... Thanks so much to @moontear!

It allows me to use the current items property bag and nullifies the key each time so it doesn't retain the value.

public override void ItemUpdating(SPItemEventProperties properties)
{    
    SPListItem currentItem = properties.List.Items.GetItemById(properties.ListItem.ID);

    //I had to null out the key or I would have conflicts the next time the event is triggered
    currentItem.Properties["DateChanged"] = null;
    currentItem.SystemUpdate(false);

    currentItem.Properties["DateChanged"] = true;
    currentItem.SystemUpdate(false);
}

public override void ItemUpdated(SPItemEventProperties properties)
{
    if (Convert.ToBoolean(Convert.ToBoolean(currentItem.Properties["DateChanged"]))
        {
            //do something
        }
}
0
votes

Enable Item version (optionally max to 2 or 5)

and on ItemUpdated use:

object oldValue = properties.ListItem.Versions[1]["FieldName"];
object currentValue = properties.ListItem.Versions[0]["FieldName"];

The versions index 0 will always return the current item version.(this happened for my test with about 5 modifications, I recommend test it again :) )

0
votes

Create one column in the Main List, where values are changes say "OLDVALUES". Set that field using the properties.ListItem["OLDVALUES"]=Value1+";" +Value2+";" +Value3+";"; in ItemUpdating event (take Value1, Values2 and Value3 using the properties.ListItem["Value1"] and so on).

Now in Item Updating, use like

string oldValue = properties.ListItem["OLDVALUES"].ToString(); 

and slip in array and then you can set global variables and access them in your code. Remember its a SandBox solution approach for event receivers, not for farm solution.