Update
multiple items in SharePoint List or Document Library using SPServices jQuery
library in SharePoint
In SharePoint 2013 we can utilize client side code to
update the list item. The client side scripting includes:
- JSOM (JavaScript Object Model)
- REST API using JQuery AJAX call
- SPServices JavaScript Add-on
In this article, i will explain How to use SPUpdateMultipleListItems
function of SPServices JavaScript add-on to Update or Delete Multiple/Single
Items.
The details of $().SPServices.
SPUpdateMultipleListItems
operation are:
[pre class="brush:jscript" ]
$().SPServices({
webURL: {web URL},
async: {true/false},
listName: {List Name},
CAMLQuery: {caml Query},
batchCmd: {"Delete" or "Update"},
valuepairs: {key/value pair of columns},
ID: {Id of the row, when update record},
completefunc: function (xData, Status) {
console.log('List items has been updated');
}
});
[/pre]
- webURL: This allows you to change the context for the operation to a
different site
- async: you can pass true or false
- listName: Name of the list
from which data needs to be fetched
- CAMLQuery: The CAMLQuery
option allows you to specify the filter on the list. CAMLQuery here should
contain valid CAML such as:
[pre class="brush:jscript" ]
var camlQuery = '';
camlQuery += '';
camlQuery += "Yes;
camlQuery += '';
camlQuery += '';
[/pre]
- batchCmd: We can set value as "Delete" or "Update".
- valuepairs: It is an optional
parameter and used to define Key/Value pair. If you specify updates then
valuepairs should not be specified.
valuepairs: [["Title", "Amit Kumar"],
["Technologies", ".NET, SharePoint, AngularJS"]]
- ID: It’s an optional parameter
and needs to be used with valuepairs
when we are using the batchCmd as "Update".
Update single item the SharePoint List using $().SPServices.
SPUpdateMultipleListItems operation with valuepairs
option:
[pre class="brush:jscript" ]
var camlQuery = '
';
camlQuery += '';
camlQuery += "Yes;
camlQuery += '';
camlQuery += '';
$().SPServices({
async: false,
batchCmd: "Update",
listName: "AmitKumar_TestList",
ID: 1,
valuepairs: [["Title","Amit Kumar"]],
CAMLQuery: camlQuery,
completefunc: function (xData, Status) {
console.log('List items has been updated');
}
});
[/pre]
In the above code block, we are using "Update" batchCmd with ID attribute to update single item.
Delete mulitple item the SharePoint List using $().SPServices. SPUpdateMultipleListItems operation with valuepairs option:
[pre class="brush:jscript" ]
var camlQuery = '
';
camlQuery += '';
camlQuery += "Yes;
camlQuery += '';
camlQuery += '';
$().SPServices({
async: false,
batchCmd: "Delete",
listName: "AmitKumar_TestList",
CAMLQuery: camlQuery,
completefunc: function (xData, Status) {
console.log('List items has been updated');
}
});
[/pre]
In the above code block, we are using "Delete" batchCmd with CAMLQuery attribute to delete multiple items.
The SPUpdateMultipleListItems function works like this:
It first calls GetListItems with the provided CAMLQuery to find all off the items which meet the criteria
Then the function calls
UpdateListItems and updates all of the items found with the values provided.
Reference: https://github.com/sympmarc/SPServices/wiki/SPUpdateMultipleListItems
Comments
Also See: AngularJS Training in Chennai