使用事件接收器更新SharePoint库中的文档元数据

时间:2012-08-24 12:36:37

标签: c# events sharepoint checkout checkin

我有一个事件接收器,它应该将元数据添加到SharePoint库中的文档。事件接收器在ItemUpdated上触发,并且应该向字段添加URL。

以下代码完美无缺,除了一个小问题,当替换文档时,它仍然被检出。

因此,当我向库中添加新文档时,事件接收器会添加元数据并签入文档。 但是,当我上传一个具有相同名称的新文档,从而替换它时,该文档没有任何元数据并被检出。当我手动签入文档时,会添加元数据。

这是我的代码。

SPField projectNameDocField = Methods.GetField(web, sharedDocumentList, Field.projectURLInternal);

SPListItem projectSiteItem = GetProjectSiteItem(web);

SPField projectNameSiteField = Methods.GetField(web.ParentWeb, projectSiteItem.ParentList, Field.projectURLInternal);

if (listItem.File.CheckOutType == SPFile.SPCheckOutType.None)
{
    listItem.File.CheckOut();

    if (projectSiteItem[projectNameSiteField.Id] != null)
    {
        SPFieldUrlValue projectNameUrlField = new SPFieldUrlValue();
        projectNameUrlField.Description = web.Title;
        projectNameUrlField.Url = web.Url;

        listItem[projectNameDocField.Id] = projectNameUrlField;
        listItem.Update();

        SPListItem updatedListItem = sharedDocumentList.GetItemById(listItem.ID);

        if (updatedListItem.File.CheckOutType != SPFile.SPCheckOutType.None)
        {
            updatedListItem.File.CheckIn("Automatisk uppdatering av metataggar", SPCheckinType.MinorCheckIn);
        }
    }
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您的事件接收器仅在项目更新后触发。当您上传文档以替换现有文档时,在检入更改之前不会触发事件。

尽管我还没有测试过,但你可能会对ItemUpdating event感到更幸运。这可能会在办理登机手续之前解雇。

您也可以查看其他人full list of events here