使用CAML / jQuery将多个记录更新为Sharepoint(批量更新)

时间:2016-01-06 20:57:14

标签: javascript jquery ajax sharepoint caml

我正在尝试使用jQuery和CAML更新Share Point中的多个记录。我可以使用此代码轻松更新单个记录,但对于此项目,我需要一次更新20个产品。我想我可以循环并调用这个函数20次,但我认为必须有一个更优雅的解决方案,希望能在一个请求中完成任务。

每个调查都有20个选择框,名为" Prod_1"," Prod_2",...," Prod_20"。

function saveProducts() {

        var sList = "ProductData"
        var i = 1;

        var sQry = "";
        sQry += "<Batch OnError='Continue'>";
        sQry += "   <Method ID='1' Cmd='Update'>";
        sQry += "     <Field Name='ID'>" + $("#uniqueID").val() + "</Field>";
        sQry += "     <Field Name='Offered'>" + $("#Prod_1").val() + "</Field>";
        sQry += "   </Method>";
        sQry += "</Batch>";

        var request = "";
        request += "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'";
        request += " xmlns:xsd='http://www.w3.org/2001/XMLSchema'";
        request += " xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>";
        request += " <soap:Body>";
        request += " <UpdateListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>";
        request += " <listName>" + sList + "</listName>";
        request += " <updates>" + sQry + "</updates>";
        request += " </UpdateListItems>";
        request += " </soap:Body>";
        request += "</soap:Envelope>";

        $.ajax({
            async: false, url: sURL, type: "POST", dataType: "xml", data: request, contentType: "text/xml; charset=\"utf-8\"",
            beforeSend: function (xhr) { xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems"); },
            complete: function processSaveProducts(xData, status) {
                //alert("Save Status: " + status);
            }
        });
    }

有没有办法通过单次更新而不是运行此代码20次?谢谢!

1 个答案:

答案 0 :(得分:2)

您可以在同一<Method>

中嵌套多个<Batch>个节点
<Batch OnError="Continue" ListVersion="1" 
ViewName="270C0508-A54F-4387-8AD0-49686D685EB2">
   <Method ID="1" Cmd="Update">
      <Field Name="ID">4<Field>
      <Field Name="Field_Name">Value</Field>
   </Method>
   <Method ID="2" Cmd="Update">
      <Field Name="ID" >6</Field>
      <Field Name="Field_Name">Value</Field>
   </Method>
</Batch>

更多信息: https://msdn.microsoft.com/en-us/library/lists.lists.updatelistitems(v=office.12).aspx