Batch Update list items In SharePoint List Using JSOM/CSOM

%3CLINGO-SUB%20id%3D%22lingo-sub-269133%22%20slang%3D%22en-US%22%3EBatch%20Update%20list%20items%20In%20SharePoint%20List%20Using%20JSOM%2FCSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-269133%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All%2C%3CBR%20%2F%3EI'm%20trying%20to%20update%26nbsp%3Bmultiple%20fields%26nbsp%3B%20using%20CSOM.%20It%20works%20for%20few%20items%20but%20when%20i%20try%20t%20on%20items%20over%20700%20items%20i'm%20getting%20the%20error%20below%3A-%3CBR%20%2F%3E%3CSTRONG%3Ethe%20request%20message%20is%20too%20big.%20The%20server%20does%20not%20allow%20messages%20larger%20than%202097152I%3CBR%20%2F%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EThis%20number%20of%20items%20doesn't%20look%20large%20at%20all%20%3A(%3C%2Fimg%3E%3CBR%20%2F%3EI%20think%20the%20solution%20will%20be%20to%20update%20them%20in%20batches%20using%20the%20code%20below.%3CBR%20%2F%3EThe%20issue%20i'm%20getting%20using%20the%20below%20code%20is%20if%20the%20id%20of%20the%20item%20doesn't%20exist%20it%20fails.%3CBR%20%2F%3EWhat%20would%20be%20the%20best%20approach%20to%20loop%20through%20the%20existing%20item%20ids%20and%20update%20accordingly%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Efunction%20updateListItems()%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20var%20itemArray%20%3D%20%5B%5D%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20var%20clientContext%20%3D%20SP.ClientContext.get_current()%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20var%20oList%20%3D%20clientContext.get_web().get_lists().getByTitle('TestList')%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Efor(var%20i%20%3D%201%3B%20i%26lt%3B%3D%205%3B%20i%2B%2B)%7B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20var%20oListItem%20%3D%20oList.getItemById(i)%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20oListItem.set_item('Title'%2C%20'My%20Updated%20Item!')%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20oListItem.update()%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20itemArray%5Bi%5D%20%3D%20oListItem%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20clientContext.load(itemArray%5Bi%5D)%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3EclientContext.executeQueryAsync(onQuerySucceeded%2C%20onQueryFailed)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%7D%3CBR%20%2F%3Efunction%20onQuerySucceeded()%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20alert('Items%20Updated')%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3Efunction%20onQueryFailed(sender%2C%20args)%20%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ealert('Request%20failed.%20'%20%2B%20args.get_message()%20%2B%20'%5Cn'%20%2B%20args.get_stackTrace())%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-291648%22%20slang%3D%22en-US%22%3ERe%3A%20Batch%20Update%20list%20items%20In%20SharePoint%20List%20Using%20JSOM%2FCSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-291648%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F89836%22%20target%3D%22_blank%22%3E%40Patrick%20Rote%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20according%20to%20your%20first%20error%20message%2C%20it%20looks%20like%20the%20problem%20is%20that%20your%26nbsp%3Brequest%20size%20is%20too%20large%20(2MB)%2C%20not%20that%20you're%20experiencing%20a%20timeout%20in%20your%20client%20context.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you%20wanted%20to%20run%20executeQueryAsync%20for%20every%20item%20you%20update%2C%20you%20could%20just%20move%26nbsp%3B%3C%2FP%3E%3CP%3EclientContext.executeQueryAsync(updateMultipleListItemsSuccess%2C%20updateMultipleListItemsFailed)%3B%3C%2FP%3E%3CP%3Einto%20the%20code%20block%20above%20just%20after%20you%20load.%20Otherwise%2C%20you%20could%20use%20a%20pattern%20like%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F14993%22%20target%3D%22_blank%22%3E%40Fredrik%20%C3%96hrn%3C%2FA%3E%26nbsp%3Bsuggested%20and%20update%20your%20loop%20to%20executeQueryAsync%20in%26nbsp%3Bwhatever%20batch%20size%20you'd%20like%20(try%2050).%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-290877%22%20slang%3D%22en-US%22%3ERe%3A%20Batch%20Update%20list%20items%20In%20SharePoint%20List%20Using%20JSOM%2FCSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-290877%22%20slang%3D%22en-US%22%3E%3CP%3ENo%20replies%20from%20anyone%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-290549%22%20slang%3D%22en-US%22%3ERe%3A%20Batch%20Update%20list%20items%20In%20SharePoint%20List%20Using%20JSOM%2FCSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-290549%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20again%2C%3C%2FP%3E%3CP%3EI'm%20not%20sure%20what%20changed%20.%20setting%20the%20timeout%20to%20infinite%20worked%20.But%20now%20it%20doesn't%20work%20any%20more.%20%3A(%3C%2Fimg%3E%3C%2FP%3E%3CP%3ESee%20my%20code%20below.%20How%20can%20i%20batch%20update%20the%20items%20and%20calling%26nbsp%3B%3CSPAN%3EexecuteQueryAsync%20each%20time.%20This%20is%20where%20i%20need%20help%20with.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EThanks%20in%20Advance%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Efunction%20updateMultipleListItems()%3CBR%20%2F%3E%7B%3CBR%20%2F%3Evar%20itemArray%20%3D%20%5B%5D%3B%3CBR%20%2F%3Evar%20clientContext%20%3D%20SP.ClientContext.get_current()%3B%3C%2FP%3E%3CP%3E%2F%2FStop%20it%20from%20timing%20out%20(infinite)%20but%20doesn't%20work%3CBR%20%2F%3EclientContext.RequestTimeout%20%3D%20-1%3B%3C%2FP%3E%3CP%3Evar%20oList%20%3D%20clientContext.get_web().get_lists().getByTitle('bers')%3B%3C%2FP%3E%3CP%3Ewhile(ListItemToBeUpdated.moveNext())%3CBR%20%2F%3E%7B%3CBR%20%2F%3Evar%20oItem%20%3D%20ListItemToBeUpdated.get_current()%3B%3CBR%20%2F%3Evar%20oListItem%20%3D%20oList.getItemById(oItem.get_id())%3B%3CBR%20%2F%3Econsole.log(oListItem)%3B%3CBR%20%2F%3Econsole.log(oItem.get_id())%3B%3C%2FP%3E%3CP%3EoListItem.set_item('UpdateField'%2C%20'Update')%3B%3CBR%20%2F%3EoListItem.update()%3B%3CBR%20%2F%3Econsole..log(%22updated%20yes%22)%3B%3CBR%20%2F%3EitemArray.push(oListItem)%3B%3CBR%20%2F%3EclientContext.load(itemArray%5BitemArray.length-1%5D)%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3EclientContext.executeQueryAsync(updateMultipleListItemsSuccess%2C%20updateMultipleListItemsFailed)%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-281402%22%20slang%3D%22en-US%22%3ERe%3A%20Batch%20Update%20list%20items%20In%20SharePoint%20List%20Using%20JSOM%2FCSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-281402%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20guys%20.I%20ended%20up%20using%20js%20and%20csom%20as%20that%20was%20what%20i%20was%20after%20and%20used%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CSPAN%3EclientContext%3C%2FSPAN%3E%3CSPAN%3E.RequestTimeOut%3C%2FSPAN%3E%20%3CSPAN%3E%3D%3C%2FSPAN%3E%3CSPAN%3E-1%3C%2FSPAN%3E%3CSPAN%3E%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Eto%20eliminate%20the%20timeout%20issue%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-269844%22%20slang%3D%22en-US%22%3ERe%3A%20Batch%20Update%20list%20items%20In%20SharePoint%20List%20Using%20JSOM%2FCSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-269844%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Patrick%2C%3C%2FP%3E%3CP%3EYou%20could%20run%20something%20like%20this.%20The%20example%20is%20in%20C%23%20but%20you%20can%20easily%20change%20it%20to%20work%20with%20your%20JavaScript%20code.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Evar%20count%20%3D%20%3CCOLLECTION%20count%3D%22%22%3E%3B%3CBR%20%2F%3Evar%20maxChunkSize%20%3D%20200%3B%3CBR%20%2F%3Evar%20start%20%3D%200%3B%3CBR%20%2F%3Evar%20chunkSize%20%3D%20count%20%26lt%3B%20maxChunkSize%20%3F%20count%20%3A%20maxChunkSize%3B%3CBR%20%2F%3Evar%20end%20%3D%20start%20%2B%20chunkSize%20%26lt%3B%20count%20%3F%20start%20%2B%20chunkSize%20%3A%20count%3B%3CBR%20%2F%3Evar%20shouldRun%20%3D%20true%3B%3CBR%20%2F%3Ewhile%20(shouldRun)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EshouldRun%20%3D%20end%20%3D%3D%20count%20%3F%20false%20%3A%20true%3B%3CBR%20%2F%3Efor%20(int%20i%20%3D%20start%3B%20i%20%26lt%3B%20end%3B%20i%2B%2B)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%3CYOUR%20code%3D%22%22%3E%3CBR%20%2F%3E%7D%3C%2FYOUR%3E%3C%2FCOLLECTION%3E%3C%2FP%3E%3CP%3Ectx.ExecuteQuery()%3B%3CBR%20%2F%3Estart%20%3D%20end%3B%3CBR%20%2F%3Eend%20%3D%20start%20%2B%20chunkSize%20%26lt%3B%20count%20%3F%20start%20%2B%20chunkSize%20%3A%20count%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-269721%22%20slang%3D%22en-US%22%3ERe%3A%20Batch%20Update%20list%20items%20In%20SharePoint%20List%20Using%20JSOM%2FCSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-269721%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%20All%2C%3CBR%20%2F%3EI'm%20currently%20getting%20this%20error%20below%20when%20i%20try%20and%20update%20more%20than%20800%20records%20at%20once%3CBR%20%2F%3E%3CSTRONG%3Ethe%20request%20message%20is%20too%20big.%20The%20server%20does%20not%20allow%20messages%20larger%20than%202097152I%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EI%20need%20help%20with%20batching%20the%20request%20in%20100%20or%20more%20to%20help%20eliminate%20the%20error.%3C%2FP%3E%3CBLOCKQUOTE%3E%3CP%3ESee%20my%20snippet%20below%3C%2FP%3E%3CP%3Efunction%20updateListItems()%20%7B%3CBR%20%2F%3E%3CBR%20%2F%3Evar%20itemArray%20%3D%20%5B%5D%3B%3CBR%20%2F%3Evar%20clientContext%20%3D%20SP.ClientContext.get_current()%3B%3CBR%20%2F%3Evar%20oList%20%3D%20clientContext.get_web().get_lists().getByTitle('List')%3B%3CBR%20%2F%3E%3CBR%20%2F%3Evar%20camlQuery%20%3D%20new%20SP.CamlQuery()%3B%3CBR%20%2F%3EcamlQuery.set_viewXml('%3CVIEW%3E%3CQUERY%3E%3C%2FQUERY%3E%3CROWLIMIT%3E200%3C%2FROWLIMIT%3E%3C%2FVIEW%3E')%3B%3CBR%20%2F%3E%3CBR%20%2F%3Ethis.collListItem%20%3D%20oList.getItems(camlQuery)%3B%3CBR%20%2F%3E%3CBR%20%2F%3EclientContext.load(oList)%3B%3CBR%20%2F%3EclientContext.load(collListItem)%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%2F*%20If%20i%20pass%20individual%20id%20it%20works%20fine%20but%20i%20want%20to%20send%20batches%20of%20100%20here%20so%20it%20would%20update%20100%20items%20twice%3CBR%20%2F%3Ethis.oListItem%20%3D%20oList.getItemById(11)%3B%3C%2FP%3E%3CP%3EoListItem.set_item(%22column%22%2C%20%22textToUpdate%22)%3B%3C%2FP%3E%3CP%3EoListItem.update()%3B%3C%2FP%3E%3CP%3EclientContext.executeQueryAsync(onQuerySucceeded%2C%20onQueryFailed)%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%2F*%20here%20i%20need%20to%20loop%20though%20the%20itemid%20instead%20of%20looping%20though%20a%20whole%20sequence%20number%3CBR%20%2F%3Efor(var%20i%20%3D%200%3B%20i%20%26lt%3B%3D%201000%3B%20i%2B%2B)%3CBR%20%2F%3E%7B%3CBR%20%2F%3Evar%20oListItem%20%3D%20oList.getItemById(i)%3B%3CBR%20%2F%3E%3CBR%20%2F%3EoListItem.set_item(%22column%22%2C%20%22textToUpdate%22)%3B%3CBR%20%2F%3EoListItem.update()%3B%3CBR%20%2F%3EitemArray%5Bi%5D%20%3D%20oListItem%3B%3CBR%20%2F%3EclientContext.load(itemArray%5Bi%5D)%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%3CBR%20%2F%3E*%2F%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3Efunction%20onQuerySucceeded()%20%7B%3C%2FP%3E%3CP%3Evar%20count%20%3D%200%3B%3CBR%20%2F%3Ecount%20%3D%20collListItem.get_count()%3B%3CBR%20%2F%3Ealert(count)%3B%3C%2FP%3E%3CP%3Evar%20listItemInfo%20%3D%20''%3B%3C%2FP%3E%3CP%3Evar%20listItemEnumerator%20%3D%20collListItem.getEnumerator()%3B%3CBR%20%2F%3E%3CBR%20%2F%3Ewhile%20(listItemEnumerator.moveNext())%20%7B%3CBR%20%2F%3Evar%20oListItem%20%3D%20listItemEnumerator.get_current()%3B%3CBR%20%2F%3E%3CBR%20%2F%3ElistItemInfo%20%2B%3D%20'%5CnID%3A%20'%20%2B%20oListItem.get_id()%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3Ealert(listItemInfo.toString())%3B%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3Efunction%20onQueryFailed(sender%2C%20args)%20%7B%3CBR%20%2F%3Ealert('Request%20failed.%20'%20%2B%20args.get_message()%20%2B%20'%5Cn'%20%2B%20args.get_stackTrace())%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3C%2FBLOCKQUOTE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EThanks%20in%20Advance%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-269566%22%20slang%3D%22en-US%22%3ERe%3A%20Batch%20Update%20list%20items%20In%20SharePoint%20List%20Using%20JSOM%2FCSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-269566%22%20slang%3D%22en-US%22%3EThanks%20guys.%20Any%20resources%20or%20help%20on%20how%20i%20can%20batch%20them%20lets%20say%20100%20each%20time%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-269180%22%20slang%3D%22en-US%22%3ERe%3A%20Batch%20Update%20list%20items%20In%20SharePoint%20List%20Using%20JSOM%2FCSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-269180%22%20slang%3D%22en-US%22%3E%3CP%3EWhat%20Marko%20suggested%20is%20correct.%20Updating%20700%20items%20at%20a%20time%20will%20just%20take%20too%20long%20and%20may%20even%20timeout%20(depending%20on%20the%20operation).%3C%2FP%3E%3CP%3EJust%20run%20the%20ExecuteQuery%20every%20time%20you%20reach%20a%20certain%20number%20of%20items%3A%2010%2C%2050%2C%20...%2C%20it's%20really%20up%20to%20you%20but%20700%20is%20probably%20too%20much%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-269141%22%20slang%3D%22en-US%22%3ERe%3A%20Batch%20Update%20list%20items%20In%20SharePoint%20List%20Using%20JSOM%2FCSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-269141%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Etry%20running%20the%20executequery%20more%20often.%20Inside%20the%26nbsp%3B%3CSPAN%3EupdateListItems%3C%2FSPAN%3E%20-function.%20Now%20you%20are%20updating%20all%20the%20list%20items%20at%20once%20and%20that%20is%20too%20much%20for%20SPO%20to%20handle.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E-Marko%3C%2FP%3E%3C%2FLINGO-BODY%3E
Frequent Contributor

Hi All,
I'm trying to update multiple fields  using CSOM. It works for few items but when i try t on items over 700 items i'm getting the error below:-
the request message is too big. The server does not allow messages larger than 2097152I

This number of items doesn't look large at all :(
I think the solution will be to update them in batches using the code below.
The issue i'm getting using the below code is if the id of the item doesn't exist it fails.
What would be the best approach to loop through the existing item ids and update accordingly?

 

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!'); 
    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());

 

}

 

Thanks in advance

9 Replies

Hi,

 

try running the executequery more often. Inside the updateListItems -function. Now you are updating all the list items at once and that is too much for SPO to handle.

 

-Marko

What Marko suggested is correct. Updating 700 items at a time will just take too long and may even timeout (depending on the operation).

Just run the ExecuteQuery every time you reach a certain number of items: 10, 50, ..., it's really up to you but 700 is probably too much

Thanks guys. Any resources or help on how i can batch them lets say 100 each time?

 

Hi All,
I'm currently getting this error below when i try and update more than 800 records at once
the request message is too big. The server does not allow messages larger than 2097152I

I need help with batching the request in 100 or more to help eliminate the error.

See my snippet below

function updateListItems() {

var itemArray = [];
var clientContext = SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('List');

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query></Query><RowLimit>200</RowLimit></View>');

this.collListItem = oList.getItems(camlQuery);

clientContext.load(oList);
clientContext.load(collListItem);

/* If i pass individual id it works fine but i want to send batches of 100 here so it would update 100 items twice
this.oListItem = oList.getItemById(11);

oListItem.set_item("column", "textToUpdate");

oListItem.update();

clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);


/* here i need to loop though the itemid instead of looping though a whole sequence number
for(var i = 0; i <= 1000; i++)
{
var oListItem = oList.getItemById(i);

oListItem.set_item("column", "textToUpdate");
oListItem.update();
itemArray[i] = oListItem;
clientContext.load(itemArray[i]);


}
*/

}

function onQuerySucceeded() {

var count = 0;
count = collListItem.get_count();
alert(count);

var listItemInfo = '';

var listItemEnumerator = collListItem.getEnumerator();

while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();

listItemInfo += '\nID: ' + oListItem.get_id();

}

alert(listItemInfo.toString());

}

function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

 

Thanks in Advance

 

 

Hi Patrick,

You could run something like this. The example is in C# but you can easily change it to work with your JavaScript code.

 

var count = <collection count>;
var maxChunkSize = 200;
var start = 0;
var chunkSize = count < maxChunkSize ? count : maxChunkSize;
var end = start + chunkSize < count ? start + chunkSize : count;
var shouldRun = true;
while (shouldRun)
{
shouldRun = end == count ? false : true;
for (int i = start; i < end; i++)
{
<your code>
}

ctx.ExecuteQuery();
start = end;
end = start + chunkSize < count ? start + chunkSize : count;
}

Thanks guys .I ended up using js and csom as that was what i was after and used 

clientContext.RequestTimeOut =-1;
to eliminate the timeout issue

Hi again,

I'm not sure what changed . setting the timeout to infinite worked .But now it doesn't work any more. :(

See my code below. How can i batch update the items and calling executeQueryAsync each time. This is where i need help with.

Thanks in Advance

 

function updateMultipleListItems()
{
var itemArray = [];
var clientContext = SP.ClientContext.get_current();

//Stop it from timing out (infinite) but doesn't work
clientContext.RequestTimeout = -1;

var oList = clientContext.get_web().get_lists().getByTitle('bers');

while(ListItemToBeUpdated.moveNext())
{
var oItem = ListItemToBeUpdated.get_current();
var oListItem = oList.getItemById(oItem.get_id());
console.log(oListItem);
console.log(oItem.get_id());

oListItem.set_item('UpdateField', 'Update');
oListItem.update();
console..log("updated yes");
itemArray.push(oListItem);
clientContext.load(itemArray[itemArray.length-1]);
}
clientContext.executeQueryAsync(updateMultipleListItemsSuccess, updateMultipleListItemsFailed);
}

 

No replies from anyone

Hi @Patrick Rote

 

So according to your first error message, it looks like the problem is that your request size is too large (2MB), not that you're experiencing a timeout in your client context.

 

If you wanted to run executeQueryAsync for every item you update, you could just move 

clientContext.executeQueryAsync(updateMultipleListItemsSuccess, updateMultipleListItemsFailed);

into the code block above just after you load. Otherwise, you could use a pattern like @Fredrik Öhrn suggested and update your loop to executeQueryAsync in whatever batch size you'd like (try 50).