【问题标题】:ListItem Value not updating on ItemUpdated, only after hitting RefreshListItem 值未在 ItemUpdated 上更新,仅在点击 Refresh 后
【发布时间】:2012-05-23 05:24:02
【问题描述】:

我是 Sharepoint 的新手。 我有一个连接到 ItemUpdated 事件的 EventReceiver,我想在一个字段中写一个文本。 当我上传文件时,事件触发正常,它通过调试代码,似乎更新但我的属性没有收到它应该接收的文本。但是,在页面上点击刷新后,我可以看到更新后的值。

这是我的代码

    public override void ItemUpdated(SPItemEventProperties properties)
    {
        base.ItemUpdated(properties);

        string folderPath = string.Empty;
        SPListItem item = properties.ListItem;
        if (item.File.ParentFolder != null)
        {
            folderPath = item.File.ParentFolder.ServerRelativeUrl;
        }

        AssignPropertyToField("Folder Name", item, folderPath);
    }        

    private void AssignPropertyToField(string fieldName, SPListItem item, string folderPath)
    {
        item[fieldName] = folderPath;

        this.EventFiringEnabled = false;
        item.SystemUpdate();
        this.EventFiringEnabled = true;
    }

提前感谢您的建议,

问候,

【问题讨论】:

    标签: sharepoint listitem event-receiver


    【解决方案1】:

    如果可能,请尝试使用 ItemUpdating 而不是 ItemUpdated

    由于 ItemUpdated 是异步的,因此您不应指望在刷新页面之前调用它。使用 ItemUpdating,请注意列表项尚未保存,因此您无需调用 SystemUpdate

    public override void ItemUpdating(SPItemEventProperties properties)
    {
        string folderPath = string.Empty;
        SPListItem item = properties.ListItem;
        if (item.File.ParentFolder != null)
        {
            folderPath = item.File.ParentFolder.ServerRelativeUrl;
        }
        properties.AfterProperties["FolderNameInternalName"] = folderPath;
    }        
    

    在您的情况下,问题是您是否能够在 ItemUpdating 事件中检索更新的父文件夹信息。我上面的示例代码将采用以前存在的文件夹信息。如果将文件移动到其他文件夹,此代码将为您提供错误的 URL。

    【讨论】:

    • 谢谢你,你说的很有道理。但是我有两个问题。 1) 我现在不在工作,所以我无法测试,但我尝试了 ItemUpdating 并收到代码为 0x81020015 的 异常。可能是因为在那个事件中,properties.ListItem 对象为 Null? 2)你提到我根本不需要调用 SystemUpdate() ,我是否也应该避免调用 Update() ?谢谢,
    • 对于#1,我不确定是什么导致了错误,但 properties.ListItem 不应该为空。对于 ItemAdding,它将为空,但对于 ItemUpdating 则不是。对于#2,这是正确的。您不需要调用 Update,因为列表项已经在更新。 ItemUpdating 事件让您可以在 Update 实际保存之前忽略任何最后一分钟的更改(或完全取消更改)。
    • 我正在尝试应用您的解决方案,但是:在 ItemUpdating 事件中,properties.AferProperties 集合的计数为 0。是否有任何具体原因,我做错了什么?跨度>
    • properties.AfterProperties.ChangedProperties.Count 将为 0,直到您更改属性。 SPItemEventDataCollection 有一个名为 Properties 的内部属性,用于维护列表中的值。虽然您无法从中获取计数,但您可以通过 Item 属性获取各个值。
    【解决方案2】:

    你可以调用 item.Update() 而不是 item.SystemUpdate()

    请注意,这种方式 ItemUpdated 事件处理程序将被调用两次,因此您需要确保仅当 item[fieldName] 与 AssignPropertyToField 中的 folderPath 不同时才进行更新,以避免无限循环。

    【讨论】:

    • 谢谢你,这是有用的信息,但仍然没有回答我的问题。不过还是谢谢你。我尝试使用 Update 而不是 UpdateSystem 但没有任何改变。字段保持为空,仅在页面刷新后更新
    【解决方案3】:

    您可以做的是,在定义 ItemUpdated 接收器的 elements.xml 中定义它应该同步运行。看到这个http://msdn.microsoft.com/en-us/library/ff512765.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      相关资源
      最近更新 更多