Wednesday, September 19, 2012

JQuery: Batch update the list items via Lists.asmx

Quick JQuery script to batch update the list items via Lists.asmx.

I have two lists Projects(Title, ProjectStatus) and Teams(Title, Project, ProjectStatus). Team.Project is a lookup column from Projects.Title and I want to update Teams.ProjectStatus when Projects.ProjectStatus is updated in the Projects list via EditForm.aspx. Now we can link two columns from projects list into the Teams list but can not connect them together in the Teams list, so following script was a great help. It batch updates the Teams.ProjectStatus once Project.ProjectStatus is updated.

Thanks to the this post I found 100% functionality ready to go.


<script type="text/javascript" src="/Scripts/jquery-1.8.1.min.js"></script>

<script type="text/javascript">

var strProject = "";
var strProjectStatus = "";

function PreSaveItem()
{
 strProject = document.getElementById('ctl00_m_g_f2d95e24_befb_49d2_b15f_823d71c9c758_ctl00_ctl04_ctl00_ctl00_ctl00_ctl04_ctl00_ctl00_TextField').value;
 strProjectStatus = document.getElementById('ctl00_m_g_f2d95e24_befb_49d2_b15f_823d71c9c758_ctl00_ctl04_ctl01_ctl00_ctl00_ctl04_ctl00_DropDownChoice').value;
 
 GetProjectID();
  
 return true;
}

function GetProjectID(){

var soapEnv = "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \
<soap:Body> \
    <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
        <listName>Teams</listName> \
        <query><Query><Where><Eq><FieldRef Name='Project'/><Value Type='Text'>"+ strProject +"</Value> </Eq></Where></Query></query> \
        <viewFields> \
            <ViewFields> \
                <FieldRef Name='ID' /> \
                <FieldRef Name='Title' /> \
            </ViewFields> \
        </viewFields> \
        <rowLimit>99999</rowLimit> \
        <queryOptions xmlns:SOAPSDK9='http://schemas.microsoft.com/sharepoint/soap/' ><QueryOptions/> \
        </queryOptions> \
    </GetListItems> \
</soap:Body> \
</soap:Envelope>";

jQuery.ajax({
url: "/itsglobal/_vti_bin/lists.asmx",
type: "POST",
dataType: "xml",
data: soapEnv,
complete: ResultMethod,
contentType: "text/xml; charset=\"utf-8\""
});

}

function ResultMethod(xData, status) 
{

    if (status=="success")
    {
        var oldbatch = "";
        $(xData.responseXML).find("z\\:row").each(function() 
        {
            oldbatch = AddTobatch(oldbatch, $(this).attr("ows_ID"));
        });
        
        UpdateTeamItem(oldbatch);
    }

}

function AddTobatch(oldbatch, itemId)
{

//Specify the batch query
var _batch = "<Method ID='1' Cmd='Update'><Field Name='ID'>" + itemId +"</Field><Field Name='ProjectStatus'>"+ strProjectStatus +"</Field></Method>";
oldbatch = oldbatch + _batch;

return oldbatch;
}

function UpdateTeamItem(batch) 
{
var soapEnv = "<?xml version=\'1.0\' encoding=\'utf-8\'?> \
<soap:Envelope xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\' \
xmlns:xsd=\'http://www.w3.org/2001/XMLSchema\' \
xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/\'> \
<soap:Body> \
<UpdateListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
<listName>Teams</listName> \
<updates> \
<Batch OnError='Continue'>" + batch + "</Batch> \
</updates> \
</UpdateListItems> \
</soap:Body> \
</soap:Envelope>";

$.ajax({
url: "/itsglobal/_vti_bin/lists.asmx",
beforeSend: function(xhr) { xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems");
},
type: "POST",
dataType: "xml",
data: soapEnv,
complete: updateResult,
contentType: "text/xml; charset=\"utf-8\""

});
}

function updateResult(xData, status) {
if (status != 'success' )
    alert("Update Project Status in Teams library failed");
}


</script>

JQuery: Read list Item via Lists.asmx

Following JQuery script is to get list items using Lists.asmx web service in MOSS 2007:
<script type="text/javascript" src="/Scripts/jquery-1.8.1.min.js"></script>
<script type="text/javascript">

//This function will call when OK button will be pressed on a List's default form (e.g. EditItem.aspx).

function PreSaveItem()
{
 var project = "Project A";
 
 GetProjectID(project);
 
 return true;
}

function GetProjectID(project){

var soapEnv = "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \
<soap:Body> \
    <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
        <listName>Teams</listName> \
        <query><Query><Where><Eq><FieldRef Name='Project'/><Value Type='Text'>"+ project +"</Value> </Eq></Where></Query></query> \
        <viewFields> \
            <ViewFields> \
                <FieldRef Name='ID' /> \
                <FieldRef Name='Title' /> \
            </ViewFields> \
        </viewFields> \
        <rowLimit>99999</rowLimit> \
        <queryOptions xmlns:SOAPSDK9='http://schemas.microsoft.com/sharepoint/soap/' ><QueryOptions/> \
        </queryOptions> \
    </GetListItems> \
</soap:Body> \
</soap:Envelope>";

jQuery.ajax({
url: "/itsglobal/_vti_bin/lists.asmx",
type: "POST",
dataType: "xml",
data: soapEnv,
complete: ResultMethod,
contentType: "text/xml; charset=\"utf-8\""
});

}

function ResultMethod(xData, status) 
{

    if (status=="success")
    {
        alert(status);
        $(xData.responseXML).find("z\\:row").each(function() 
        {
            alert($(this).attr("ows_ID"));
        });
    }

}

</script>
I have copied this script from the internet and have tweaked it a bit to work with JQuery 1.8 version.