奇怪的SharePoint ItemUpdating行为

时间:2016-08-17 14:56:24

标签: c# sharepoint-2010 event-receiver

我有一个SharePoint列表,我在其中注册了一个自定义的ItemUpdating事件接收器,但我在这个解决方案中看到了一些非常奇怪的行为。如果我向base.ItemUpdating以外的事件接收器添加任何代码,则会发生此行为。

如果我调试事件接收器,我会看到属性.AfterProperties具有在字段上输入的所有值,而properties.ListItem具有原始项目。但是一旦ER完成运行并且页面重新加载,就不会保存任何内容,它只会返回到我更改值之前的状态。更奇怪的是,如果我去手动设置类似于下面的后属性它可以工作,并且更新得到正确保存。所以基本上事件接收器让我负责对项目进行任何更改,但这不是ItemUpdating的正常行为。有谁知道可能导致这种情况的原因?

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);
}

1 个答案:

答案 0 :(得分:0)

是不是因为你根本没有保存当前项目,如下所示:

item.update();