Saturday, February 7, 2015

Batch update list items using client scripts

Share/Save/Bookmark
We know that SPServices has a way to create new or update items but when in the case of creating multiple items(say 10), calling the same query 10 times is not going to be good from performance perspective. so lets use batch command to do it from client side as below

var query_string = "";
for(int i=1;i<=5;i++)
{
var title = $('input#title'+i).val();
var desc = $('input#desc'+i).val();
query_string += "<Method ID='1' Cmd='New'>";
query_string += "<Field Name='Title'>"+title+"</Field>";
query_string += "<Field Name='Description'>"+desc+"</Field>";
query_string += "</Method>";

}
query_string += "
";

$().SPServices({
    operation: "UpdateListItems",
    async: false,
    batchCmd="New",
    listName: "Requests",
    updates: query_string,
    completefunc: function(xData, Status) {
        alert(xData.responseText);
    }
});

for update operations, batchCmd should be updated as 'Update' and same in the query string as well.


Subscribe

No comments:

Post a Comment