Sharepoint - How to update multiple items in sharepoint list online using javascript
I have a list called rohit_list1
on which i am updating title
for all the items with ID
greater than 15
.
You Write your own CAML query to get ID's.
Design :
<input type="button" value="updateMultipleListItems" onclick="getItemsToBeUpdated()" />
JSOM Code :
<script type="text/javascript">
var collListItemToBeUpdated = "";
var listItemToBeUpdated = "";
function getItemsToBeUpdated() <--- this function called on button click to get ID's
{
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('rohit_list1');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Geq>' +
'<FieldRef Name=\'ID\'/><Value Type=\'Number\'>15</Value>' +
'</Geq></Where></Query></View>');
collListItemToBeUpdated = oList.getItems(camlQuery);
clientContext.load(collListItemToBeUpdated);
clientContext.executeQueryAsync(getItemsToBeUpdatedSuccess, getItemsToBeUpdatedFailed);
}
function getItemsToBeUpdatedSuccess()
{
ListItemToBeUpdated= collListItemToBeUpdated.getEnumerator();
updateMultipleListItems();
}
function getItemsToBeUpdatedFailed(sender, args)
{
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
function updateMultipleListItems() <--- this function called on successfully getting ID's
{
var itemArray = [];
var clientContext = SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('rohit_list1');
while(ListItemToBeUpdated.moveNext())
{
var oItem = ListItemToBeUpdated.get_current();
var oListItem = oList.getItemById(oItem.get_id());
oListItem.set_item('Title', 'Updated ...!');
oListItem.update();
itemArray.push(oListItem);
clientContext.load(itemArray[itemArray.length-1]);
}
clientContext.executeQueryAsync(updateMultipleListItemsSuccess, updateMultipleListItemsFailed);
}
function updateMultipleListItemsSuccess()
{
alert('Items Updated');
}
function updateMultipleListItemsFailed(sender, args)
{
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
Since you haven't told us which API you prefer, below is demonstrated how to retrieve and update list items using JSOM API:
function getListItems(listTitle)
{
var deferred = $.Deferred();
var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle(listTitle);
var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
ctx.load(items);
ctx.executeQueryAsync(function() {
deferred.resolve(items);
},
function(sender,args){
deferred.reject(sender, args);
});
return deferred.promise();
}
function updateListItems(items)
{
var deferred = $.Deferred();
var ctx = items.get_context();
var itemProperties = {'Title': 'John Doe'};
//prepare to update list items
items.get_data().forEach(function(item){
for(var propName in itemProperties) {
item.set_item(propName, itemProperties[propName])
}
item.update();
});
//submit request
ctx.executeQueryAsync(function() {
deferred.resolve(items);
},
function(sender,args){
deferred.reject(sender, args);
});
return deferred.promise();
}
Usage
//update Title for all items in Contacts list
getListItems('Contacts').then(updateListItems).then(logSuccess,logError);
function logError(sender,args){
console.log('An error occured: ' + args.get_message());
}
function logSuccess(sender,args){
console.log('Updated.');
}