5

I want to update 4 items in the same time in my SharePoint custom list using JavaScript. I am using a CAML query to get the item witch I want to update. Can anybody please help me and write a function to do that?

3 Answers 3

11

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>
2
  • 1
    You can also refer this website -- vrdmn.com/2013/07/batch-operations-using-javascript.html Commented May 8, 2015 at 15:06
  • Thank you so much. I modified you code a little bit because your code change all the item to same thing but i would give a defferent value to the items i want to update in the same time. but your code inspired me to do what i want Commented May 10, 2015 at 18:26
4

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.');
}
1
  • 1
    the best answer for topic
    – egidiocs
    Commented Apr 1, 2016 at 1:48
0

Yes it is possible to batch update or create List Items


function updateListItems() {
    var itemArray = [];
    var clientContext = SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('TestList');

for(var i = 1; i<= 5; i++){    
    var oListItem = oList.getItemById(i);  
    oListItem.set_item('Title', 'My Updated Item!' + i);  
    oListItem.update();
    itemArray[i] = oListItem;
    clientContext.load(itemArray[i]);
}
clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);

} function onQuerySucceeded() { alert('Items Updated'); } function onQueryFailed(sender, args) {

alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());

} updateListItems();

2
  • Thank you so much for you kindly answer. Every thing was ok before the code goes in onQueryFailed(sender, args). It says the object dosent found Why is that? Commented May 10, 2015 at 12:14
  • 1
    @SinanKachachi May be you haven't passed the list name correctly OR the Item with ID you are trying to get doesn't exists. you can check exception message or stack strace from more details Commented May 11, 2015 at 3:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.