how to loop through item id and update items via CSOM

%3CLINGO-SUB%20id%3D%22lingo-sub-291494%22%20slang%3D%22en-US%22%3Ehow%20to%20loop%20through%20item%20id%20and%20update%20items%20via%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-291494%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EHere%20is%20my%20snippet%20code%20below.My%20aim%20is%20to%20simply%20loop%20through%20item%20ids%20and%20update%20the%20list%20in%20batches.%20I%20need%20help%20with%20using%20CSOM%20to%20do%20this%20I%20need%20this%20as%20currently%20when%20i%20send%20a%20batch%20of%20over%20800%20items%20i%20get%20timeout.%20Thanks%20in%20Advance%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Evar%20items%3B%3CBR%20%2F%3Evar%20oList%3B%3CBR%20%2F%3Evar%20oListItem%3CBR%20%2F%3Efunction%20getItemsToBeUpdated()%3CBR%20%2F%3E%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%24('%23lblprogress').text(%22Updating%20items%20please%20wait....%20%22)%3B%3CBR%20%2F%3E%26nbsp%3B%20%24('%23lblprogress').show()%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20var%20siteUrl%20%3D%20'%2Fsites%2FMySite'%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20var%20clientContext%20%3D%20new%20SP.ClientContext(siteUrl)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2F%2Fvar%20clientContext%20%3D%20new%20SP.ClientContext.get_current()%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20oList%20%3D%20clientContext.get_web().get_lists().getByTitle('PTest')%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3Bvar%20camlQuery%20%3D%20new%20SP.CamlQuery()%3B%3CBR%20%2F%3E%26nbsp%3BcamlQuery.set_viewXml('%3CVIEW%3E%3CQUERY%3E%3C%2FQUERY%3E%3CROWLIMIT%3E100%3C%2FROWLIMIT%3E%3C%2FVIEW%3E')%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20items%20%3D%20oList.getItems(camlQuery)%3B%3CBR%20%2F%3E%26nbsp%3BclientContext.load(items)%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20clientContext.executeQueryAsync(updateMultipleListItemsSuccess%2C%20updateMultipleListItemsFailed)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%20%2F%2FThis%20works%20but%20i%20need%20to%20send%2017%20items(using%20the%20itemid%20and%20then%2013%20items%20out%20of%2030%20for%20example%20etc..)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20for%20(var%20i%20%3D%201%3B%20i%20%26lt%3B%3D%2017%3B%20i%2B%2B)%20%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20this.oListItem%20%3D%20oList.getItemById(i)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20oListItem.set_item('Update'%2C%20'Force')%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20oListItem.update()%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20clientContext.executeQueryAsync(updateMultipleListItemsSuccess%2C%20updateMultipleListItemsFailed)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2F*for%20(var%20i%20%3D%2015%3B%20i%20%26lt%3B%3D%2030%3B%20i%2B%2B)%20%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20this.oListItem%20%3D%20oList.getItemById(i)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20oListItem.set_item('Force_x0020_Update'%2C%20'Force%20Update')%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20oListItem.update()%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20clientContext.executeQueryAsync(updateMultipleListItemsSuccess%2C%20updateMultipleListItemsFailed)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D*%2F%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%2F%2FI%20can%20get%20the%20id%20here%20and%20use%20it%20onSucess%20but%20when%20i%20try%20updating%20here%20it%20doesnt%20work%3CBR%20%2F%3Efunction%20getID()%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%20var%20itemEnumerator%20%3D%20items.getEnumerator()%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20while(itemEnumerator.moveNext())%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20var%20item%20%3D%20itemEnumerator.get_current()%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2F%2F%20You%20now%20have%20the%20item%20you%20were%20looking%20for!%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20console.log(item.get_item(%22ID%22))%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%3CBR%20%2F%3Efunction%20updateMultipleListItemsSuccess()%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%20getID()%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24('%23lblprogress').text(%22Items%20Updated%20Successfully%22)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2F%2Falert('Items%20Updated')%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24('%23reset').show()%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20function%20updateMultipleListItemsFailed(sender%2C%20args)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24('%23lblprogress').hide()%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24('%23lblerror').text('Request%20failed.%20'%20%2B%20args.get_message()%20%2B%20'%5Cn'%20%2B%20args.get_stackTrace())%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24('%23reset').show()%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-291494%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-292018%22%20slang%3D%22en-US%22%3ERe%3A%20how%20to%20loop%20through%20item%20id%20and%20update%20items%20via%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-292018%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20Joel%20and%20I%20get%20that.%20My%20above%20code%20works.%3CBR%20%2F%3EI'm%20just%20to%20on%20how%20to%20approach%20this%20dynamically.%20If%20you%20look%20at%20my%20code%20above%20i'm%20not%20iterating%20through%20the%20item%20ids.%3C%2FP%3E%3CP%3EI%20need%20a%20something%20that%20can%20iterate%20through%20this%20ids%20and%20then%20send%20them%20for%20update%20in%20batches.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECheers%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-291495%22%20slang%3D%22en-US%22%3ERe%3A%20how%20to%20loop%20through%20item%20id%20and%20update%20items%20via%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-291495%22%20slang%3D%22en-US%22%3EPatrick%2C%20try%20reducing%20the%20size%20of%20the%20batch%20to%20avoid%20the%20timeout%20issue.%20Try%20with%20for%20example%2010%20and%20then%20increase%20it%20if%20you%20need%3C%2FLINGO-BODY%3E
Highlighted
Contributor

Here is my snippet code below.My aim is to simply loop through item ids and update the list in batches. I need help with using CSOM to do this I need this as currently when i send a batch of over 800 items i get timeout. Thanks in Advance

 

var items;
var oList;
var oListItem
function getItemsToBeUpdated()
{

  $('#lblprogress').text("Updating items please wait.... ");
  $('#lblprogress').show();


    var siteUrl = '/sites/MySite';


    var clientContext = new SP.ClientContext(siteUrl);
    //var clientContext = new SP.ClientContext.get_current();
   
     oList = clientContext.get_web().get_lists().getByTitle('PTest');
   
 var camlQuery = new SP.CamlQuery();
 camlQuery.set_viewXml('<View><Query></Query><RowLimit>100</RowLimit></View>');
    items = oList.getItems(camlQuery);
 clientContext.load(items);   
    clientContext.executeQueryAsync(updateMultipleListItemsSuccess, updateMultipleListItemsFailed);
        
   
   //This works but i need to send 17 items(using the itemid and then 13 items out of 30 for example etc..)
    for (var i = 1; i <= 17; i++) {

        this.oListItem = oList.getItemById(i);

        oListItem.set_item('Update', 'Force');

        oListItem.update();
       
        clientContext.executeQueryAsync(updateMultipleListItemsSuccess, updateMultipleListItemsFailed);
        

    }
   
    /*for (var i = 15; i <= 30; i++) {

        this.oListItem = oList.getItemById(i);

        oListItem.set_item('Force_x0020_Update', 'Force Update');

        oListItem.update();
       
        clientContext.executeQueryAsync(updateMultipleListItemsSuccess, updateMultipleListItemsFailed);
        

    }*/

}

//I can get the id here and use it onSucess but when i try updating here it doesnt work
function getID() {
  
   var itemEnumerator = items.getEnumerator();
    while(itemEnumerator.moveNext()){
        var item = itemEnumerator.get_current();
       
        // You now have the item you were looking for!
         console.log(item.get_item("ID"));
        
    }
  

}


function updateMultipleListItemsSuccess() {
  
   getID();

    $('#lblprogress').text("Items Updated Successfully");
    //alert('Items Updated');
    $('#reset').show();
   
  

}

    function updateMultipleListItemsFailed(sender, args)
    {   

     $('#lblprogress').hide();
     $('#lblerror').text('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
     $('#reset').show();

    }

2 Replies
Highlighted
Patrick, try reducing the size of the batch to avoid the timeout issue. Try with for example 10 and then increase it if you need
Highlighted

Thanks Joel and I get that. My above code works.
I'm just to on how to approach this dynamically. If you look at my code above i'm not iterating through the item ids.

I need a something that can iterate through this ids and then send them for update in batches.

 

Cheers