0

I have a SharePoint list where I register a custom ItemUpdating event receiver but I am seeing some really strange behavior in this solution. This behavior occurs if I add any code to the event receiver other than base.ItemUpdating.

What happens is if I debug the event receiver I can see that properties.AfterProperties has all the values entered on the field and properties.ListItem has the original item. But once the ER finishes running and the page reloads nothing is saved and it just returns to what it was before I changed values. Even more weird, if I go and manually set the after properties similar to below it works and the updates are saved correctly. So basically the event receiver is making me responsible to do any changes to the item but this is not normal behavior for ItemUpdating. Does anyone have any idea what might cause this?

public override void ItemUpdating(SPItemEventProperties properties)
{
    var recurringBefore = properties.ListItem.TryGetValue<bool>(Constants.CommonFields.Recurring_STATIC);
    var recurringAfter = Convert.ToBoolean(properties.AfterProperties[Constants.CommonFields.Recurring_STATIC]);

    //This loop is the horrible fix I have done to manually update the relevant fields but this shouldn't be necessary
    var item = properties.ListItem;
    foreach (SPField key in item.Fields)
    {
        if (item[key.InternalName] != properties.AfterProperties[key.InternalName] && key.CanBeDisplayedInEditForm && properties.AfterProperties[key.InternalName] != null)
        {
            //looping through and setting the AfterProperties to what they already are makes them save? If I don't do this nothing saves
            properties.AfterProperties[key.InternalName] = properties.AfterProperties[key.InternalName].ToString();
        }
    }

    if (!recurringBefore && recurringAfter &&
        currWfStatus == Constants.WorkflowStatus.Processed)
    {
        //do some stuff
    }
    base.ItemUpdating(properties);
}
Nathan Kamenar
  • 824
  • 7
  • 30

1 Answers1

0

Is it because you are not saving current item at all, something like this:

item.update();
Jameel
  • 288
  • 1
  • 2
  • 11