1
votes

I have an application I am working on that uses an Event receiver to propagate metadata to a document library. I am trying to use SPWeb.ProcessBatchUpdate to copy the metadata back. my code looks like this:

 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();
    }
}

It generates a CAML fragment that looks like this:

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

The results look like this:

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

Nothing gets updated in the document library! What's wrong?

1

1 Answers

1
votes

http://www.dmcinfo.com/Latest-Thinking/Blog/articleType/ArticleView/articleId/8471/Using-SPWebProcessBatchData-to-Update-Document-Library-Properties.aspx Summary: When running ProcessBatchUpdate on a document library you need to include {0} and pass the server relative url of the file in each item. Use SPListItem.File.ServerRelativeUrl or SPListItem.Folder.ServerRelativeUrl.