ProcessBatchUpdate不更新文档库

时间:2013-05-16 01:56:33

标签: c# sharepoint sharepoint-2010

我正在使用的应用程序使用Event接收器将元数据传播到文档库。我正在尝试使用SPWeb.ProcessBatchUpdate来复制元数据。 我的代码看起来像这样:

 public string UpdateClosingBookDocuments(SPWeb web, SPList closingBooksDocuments, SPListItemCollection items, SPListItem sourceListItem)
    {
        StringBuilder batchDataStringBuilder = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?><ows:Batch OnError=\"Return\">");
        //string command = generateCommand(closingBooksDocuments.ID, sourceListItem, "urn:schemas-microsoft-com:office:office#");
        string command = generateCommand(closingBooksDocuments.ID, sourceListItem, null);
        foreach (SPListItem item in items)
        {
            batchDataStringBuilder.AppendFormat(command, item["ID"]);
        }
        batchDataStringBuilder.Append("</ows:Batch>");
        web.AllowUnsafeUpdates = true;
        return web.ProcessBatchData(batchDataStringBuilder.ToString());
    }

    private string generateCommand(Guid ID, SPListItem sourceListItem, string varNameSpace)
    {
        StringBuilder commandBuilder = new StringBuilder("<Method ID=\"{0}\"><SetList>");
        commandBuilder.Append(ID.ToString());
        commandBuilder.Append("</SetList>");
        commandBuilder.Append("<SetVar Name=\"Cmd\">Save</SetVar>");
        commandBuilder.Append("<SetVar Name=\"ID\">{0}</SetVar>");
        if (null != sourceListItem["Book Type"])
        {
            commandBuilder.AppendFormat("<SetVar Name=\"{1}WilliamsBookType\">{0}</SetVar>", sourceListItem["Book Type"], varNameSpace);
        }
        if (null != sourceListItem["Matter Number"])
        {
            commandBuilder.AppendFormat("<SetVar Name=\"{1}WilliamsMatterNumber\">{0}</SetVar>", sourceListItem["Matter Number"], varNameSpace);
        }
        if (null != sourceListItem["Matter Name"])
        {
            commandBuilder.AppendFormat("<SetVar Name=\"{1}WilliamsMatterName\">{0}</SetVar>", sourceListItem["Matter Name"], varNameSpace);
        }

        if (null != sourceListItem["Closing Date"])
        {
            commandBuilder.AppendFormat("<SetVar Name=\"{1}WilliamsClosingDate\">{0}</SetVar>", sourceListItem["Closing Date"], varNameSpace);
        }

        if (null != sourceListItem["Disposition Date"])
        {
            commandBuilder.AppendFormat("<SetVar Name=\"{1}WilliamsDispositionDate\">{0}</SetVar>", sourceListItem["Disposition Date"], varNameSpace);
        }
        if (null != sourceListItem["Principal Williams Party"])
        {
            commandBuilder.AppendFormat("<SetVar Name=\"{1}WilliamsPrincipalParty\">{0}</SetVar>", sourceListItem["Principal Williams Party"], varNameSpace);
        }

        if (null != sourceListItem["Principal Counter Party"])
        {
            commandBuilder.AppendFormat("<SetVar Name=\"{1}WilliamsCounterParty\">{0}</SetVar>", sourceListItem["Principal Counter Party"], varNameSpace);
        }

        if (null != sourceListItem["Other Parties"])
        {
            commandBuilder.AppendFormat("<SetVar Name=\"{1}WilliamsOtherParties\">{0}</SetVar>", sourceListItem["Other Parties"], varNameSpace);
        }
        if (null != sourceListItem["Asset Name"])
        {
            commandBuilder.AppendFormat("<SetVar Name=\"{1}WilliamsAssetName\">{0}</SetVar>", sourceListItem["Asset Name"], varNameSpace);
        }
        if (null != sourceListItem["Deal Team"])
        {
            commandBuilder.AppendFormat("<SetVar Name=\"{1}WilliamsDealTeam\">{0}</SetVar>", sourceListItem["Deal Team"], varNameSpace);
        }

        if (null != sourceListItem["Legal Department SIC"])
        {
            commandBuilder.AppendFormat("<SetVar Name=\"{1}WilliamsLegalDeptSIC\">{0}</SetVar>", sourceListItem["Legal Department SIC"], varNameSpace);
        }
        commandBuilder.Append("</Method>");

        return commandBuilder.ToString();
    }
}

它会生成一个如下所示的CAML片段:

    <?xml version="1.0" encoding="UTF-8"?>
<ows:Batch OnError="Return">
    <Method ID="1">
        <SetList>c5a3d89c-3f2b-409b-9743-1417db707586</SetList>
        <SetVar Name="Cmd">Save</SetVar>
        <SetVar Name="ID">1</SetVar>
        <SetVar Name="WilliamsBookType">Financings</SetVar>
        <SetVar Name="WilliamsMatterNumber">62626</SetVar>
        <SetVar Name="WilliamsMatterName">Debug Matter</SetVar>
    </Method>
    <Method ID="2">
        <SetList>c5a3d89c-3f2b-409b-9743-1417db707586</SetList>
        <SetVar Name="Cmd">Save</SetVar>
        <SetVar Name="ID">2</SetVar>
        <SetVar Name="WilliamsBookType">Financings</SetVar>
        <SetVar Name="WilliamsMatterNumber">62626</SetVar>
        <SetVar Name="WilliamsMatterName">Debug Matter</SetVar>
    </Method>
</ows:Batch>

结果如下:

<Results><Result ID=\"1\" Code=\"0\">\r\n</Result>\n<Result ID=\"2\" Code=\"0\">\r\n</Result>\n</Results>

文档库中没有更新任何内容!怎么了?

1 个答案:

答案 0 :(得分:1)

http://www.dmcinfo.com/Latest-Thinking/Blog/articleType/ArticleView/articleId/8471/Using-SPWebProcessBatchData-to-Update-Document-Library-Properties.aspx摘要:在文档库上运行ProcessBatchUpdate时,您需要包含{0}并在每个项目中传递文件的服务器相对URL。使用SPListItem.File.ServerRelativeUrl或SPListItem.Folder.ServerRelativeUrl。