Update item in SharePoint List or Document Library using SPServices jQuery library in SharePoint
In a previous article, How to create folder inside SharePoint List or Document Library using SPServices jQuery library in SharePoint, we already saw a $().SPServices.UpdateListItems operation to access the listing of folder.
For example, in SharePoint online site, we need to update item in SharePoint List or Document library on the basis of key field when user click on button.
To implement above requirement, we can not use the SharePoint Server Side code, so we need to utilize the Client Side Scripting – JSOM/Call to SharePoint REST API using JQuery/Use of SPServices JQuery Library.
Now, we are going to explain how to update item in SharePoint List or Document Library using SPServices jQuery library in SharePoint. SPServices JavaScript Library ($().SPServices) provides function called UpdateListItems to update content in the SharePoint list.
1. Update item the SharePoint Document Library or List using $().SPServices.UpdateListItems operation with valuepairs option:
$().SPServices({
operation: "UpdateListItems",
async: false,
batchCmd: "Update",
listName: "AmitKumar_TestList",
ID: 1,
valuepairs: [["Title","Amit Kumar"]],
completefunc: function (xData, Status) {
console.log('List items has been updated');
}
});
In the above code block, we are not using updates attributes of "UpdateListItems" operation, instead using valuepairs attribute to define the columns which needs to be updated on the basis of ID of element/row.
We can define more than one columns in valuepairs attribute, it will serve as an array of columns:
valuepairs: [["Title", "Amit Kumar"], ["Technologies", ".NET, SharePoint, AngularJS"]]
2. Update item in SharePoint Document Library or List using **$().SPServices.**UpdateListItems operation with CAML query:
var camlQuery = "<Batch OnError='Continue' PreCalc='TRUE' >" +
"<Method ID='1' Cmd='Update'>" +
"<Field Name='Title'>Amit Kumar</Field>" +
"<Field Name='Technologies'>.NET, SharePoint, AngularJS</Field>" +
"<Field Name='ID'>1</Field>" +
"</Method>" +
"</Batch>";
$().SPServices({
operation: "UpdateListItems",
async: false,
listName: "AmitKumar_TestList",
updates: camlQuery ,
completefunc: function(xData, Status) {
console.log('List items has been updated');
}
});
In the above code block, we are not using batchCmd/ID attributes of "UpdateListItems" operation, instead using updates attribute to define the columns which needs to be updated based on ID of element/row. The default for the batchCmd option is 'Update'.