2

我是 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;
    }

提前感谢您的建议,

问候,

4

3 回答 3

3

如果可能,请尝试使用 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。

于 2012-05-16T12:06:29.603 回答
1

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

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

于 2012-05-15T18:02:44.360 回答
0

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

于 2012-05-16T13:24:54.763 回答