Friday, 21 November 2014

SPServices UpdateListItems batchUpdate

Below code is updating AssignedTo field in Task List for multiple items in List

//for updating multiple items in tasks list
var update_string="<Batch OnError='Continue'>";
var count=0;

for(var i=0;i<ArrayTest.length;i++)  // ArrayTest is having list items IDs
{
count++;
update_string = update_string+"<Method ID='"+count+"' Cmd='Update'>" +
"<Field Name='ID'>"+ArrayTest[i]+"</Field>" +
"<Field Name='AssignedTo'>TestUser</Field>" +
"</Method>";  
}
update_string = update_string+"</Batch>";

$().SPServices({
operation: "UpdateListItems",
async: true,
batchCmd: "Update",
listName: "Tasks",
updates: update_string,
completefunc: function(xData, Status)
{
//alert(xData.responseText);
}
});

SPServices GetListItems Operation

//Add reference in js file
<script src="/js/jquery.SPServices-0.7.2.min.js"type="text/javascript"></script>

//CAML Query that will be used in SPServices GetListItems Operation
var funderQuery = "<Query>" +
"<Where>" +
                                "<Eq>"+
"<FieldRef Name='ID' /><Value Type='Text'>"+thisItemID+"</Value>"+
"</Eq>"+
"</Where>" +
"</Query>";

$().SPServices(
{
operation: "GetListItems",  
async: false,        
listName: "ListName",
CAMLViewFields: "<ViewFields><FieldRef Name='FieldInternalName' />     </ViewFields>",
CAMLQuery: funderQuery,
completefunc: function (xData, Status)
{      
if (Status == "success")
{      
//alert(xData.responseText);
$(xData.responseXML).SPFilterNode("z:row").each(function()
{
var field = $(this).attr("ows_FieldInternalName");
});
}
}
});