Forum Discussion
Batch Update list items In SharePoint List Using JSOM/CSOM
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
- Patrick RoteNov 23, 2018Iron Contributor
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);
}- Thomas BermanNov 27, 2018Iron Contributor
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 FredrikOhrn suggested and update your loop to executeQueryAsync in whatever batch size you'd like (try 50).
- Patrick RoteNov 25, 2018Iron Contributor
No replies from anyone