Wednesday, November 17, 2010

Bulk Update List Items using JQuery WebService

Querying and Updating the List

(1)Add a CEWP on any default.aspx page [Not on any of the List view pages]
(2)Copy paste the code and change URLs for List and Site along with Column names that you want to Update with
(3) 2 functions btnPublishAll() - Gets each List items ID's and Title  and fxnPublishList() - Updates each List item's PublishColumn to Published.
(4)Add your list(YourListName) webpart on the same page, so that Button to bulk publish all items for the List and List would be on the same page.


<script type="text/javascript">

$(function($){
        $("#btnPublishAll").click(function(){
           var soapEnv =
            "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
                <soapenv:Body> \
                     <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
                        <listName>YourListName</listName> \
                        <viewFields> \
                            <ViewFields> \
                               <FieldRef Name='ID' /> \
                               <FieldRef Name='Publish' /> \
                           </ViewFields> \
                        </viewFields> \
                    </GetListItems> \
                </soapenv:Body> \
            </soapenv:Envelope>";

        $.ajax({
            url: "http://YourServer/sites/Dev1/myDev1/_vti_bin/lists.asmx",
            type: "POST",
            dataType: "xml",
            data: soapEnv,
            complete: processResult,
            contentType: "text/xml; charset=\"utf-8\""
        }); // end $.ajax()

                function processResult(xData, status) {
                     $(xData.responseXML).find("z\\:row").each(function() {
                     //Here you get the ID's of each Item in the List and we can call Update function using these ID's
                      var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>"; //To Display each List items Title in a row
                      //$("#TestUL").append(liHtml);
                       fxnPublishList( $(this).attr("ows_ID"), "Published" ) ;
                        window.location="YourPage"; //To Refresh the page after the Update.
                                   
                  });
                } // end processResult
                                               
                                               
   }); // end click
  
 
  
}); // end ready

function fxnPublishList(ItemID,ItemNewValue)
{
    var soapEnv =
        "<?xml version='1.0' encoding='utf-8'?> \
            <soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
                <soapenv:Body> \
                     <UpdateListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
                        <listName>YourListName</listName> \
                        <viewName>AllItems</viewName> \
                        <updates><Batch OnError='Continue' PreCalc='TRUE'> \
                               <Method ID='1' Cmd='Update'> \
                                  <Field Name='Publish'>" + ItemNewValue + "</Field> \
                                  <Field Name='ID'>" + ItemID + "</Field> \
                               </Method> \
                        </Batch></updates> \
                    </UpdateListItems> \
                </soapenv:Body> \
            </soapenv:Envelope>";
   
    $.ajax({
        url: "http://YourServer/sites/Dev1/myDev1/_vti_bin/lists.asmx",
        beforeSend: function(xhr){ xhr.setRequestHeader("SOAPAction","http://schemas.microsoft.com/sharepoint/soap/UpdateListItems");},
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        contentType: "text/xml; charset=\"utf-8\""
    }); // end $.ajax()
}


</script>
<p/><p/>
<input type="button" id="btnPublishAll" value="Publish All" />


Reference:  Link to Jan Tielens Blog for querying list items.

No comments: