在itemupdating和itemupdated事件之间共享值

时间:2013-06-29 03:04:38

标签: sharepoint event-handling

我有一个包含30多个列的列表。

我已将两个事件处理程序附加到列表中。 1. ItemUpdating 2. ItemUpdated

在ItemUpdating事件中,我正在检查一个字段值以进行更改。

在ItemUpdating事件中,如果值更改,我想进行处理。我不能在这里进行比较,因为在属性之前没有在列表项中提供旧值。

处理包括很少的工作和完成后发送电子邮件。

我正在寻找一个解决方案,我可以在ItemUpdating中字段值更改时设置该位。如果set在ItemUpdated中执行处理,请检查此位。

4 个答案:

答案 0 :(得分:4)

您将无法直接共享值,您必须使用辅助方法来保存数据。

最简单的方法是使用列表项的 property bag

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

请务必使用SystemUpdate否则会遇到creating an endless loop的危险(或者事先按照该文章中的描述禁用事件触发)。

在您的ItemUpdated活动中,您只需访问specialItem.Properties["some_persisted_key"]即可访问您的价值。

答案 1 :(得分:1)

这是对我有用的完整解决方案......非常感谢@moontear!

它允许我使用当前项属性包并且每次都使该键无效,因此它不会保留该值。

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

答案 2 :(得分:0)

在主列表中创建一列,其中值为更改,例如“OLDVALUES”。使用properties.ListItem["OLDVALUES"]=Value1+";" +Value2+";" +Value3+";";事件中的ItemUpdating设置该字段(使用properties.ListItem["Value1"]取值Value1,Values2和Value3等等。)

现在在项目更新中,使用类似

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

并滑入数组,然后您可以设置全局变量并在代码中访问它们。请记住它是事件接收器的SandBox解决方案方法,而不是农场解决方案。

答案 3 :(得分:0)

启用项目版本(可选择最多2或5)

并在ItemUpdated上使用:

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

版本索引0将始终返回当前项目版本。(这发生在我的测试中,大约有5次修改,我建议再次测试:))