Oct 09 2018 11:45 PM
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
Oct 10 2018 12:03 AM
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
Oct 10 2018 01:35 AM - edited Oct 10 2018 01:36 AM
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
Oct 10 2018 01:05 PM
Oct 10 2018 11:16 PM
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
Oct 11 2018 03:56 AM - edited Oct 11 2018 03:56 AM
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;
}
Nov 01 2018 02:59 PM
Thanks guys .I ended up using js and csom as that was what i was after and used
Nov 22 2018 10:32 PM
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);
}
Nov 25 2018 04:07 AM
No replies from anyone
Nov 27 2018 07:14 AM - edited Nov 27 2018 07:17 AM
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).