0

I'm trying to update a field 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 2097152 bytes"

Is this normal as these items are small? See the code below. How can i turn the code below to update in batches?

  function updateListItems() {

    // Create an instance of the current context to return context information
    clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('Test');

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query></Query></View>');
    this.collListItem = oList.getItems(camlQuery);   
    clientContext.load(oList);
    clientContext.load(collListItem);

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));


}

function onQuerySucceeded() {

    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) 
    {   
        var item = listItemEnumerator.get_current(); 
        item.set_item("TestUnit", "TestUnit");
        //Commits changed properties of the list item
        item.update();

    }   

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onUpdateSucceeded), Function.createDelegate(this, this.onQueryFailed));

}

Thanks in Advance

3 Answers 3

0

You can use Batch Update Items from List:

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

Reference from

5
  • Thanks Sunil for your response.But the issue is if the item id that is being passed to getitemid doesn't exist it fails.
    – naijacoder
    Oct 10, 2018 at 11:22
  • How can this be achieved in batches of 50 or 100 or 500 using the script?
    – naijacoder
    Oct 10, 2018 at 21:56
  • for batch only you need iterate clientContext.load and after all items you need to clientContext.executeQueryAsync. check if(itemid) prior getting into the iteration
    – Sunil Sahu
    Oct 11, 2018 at 10:11
  • Thanks Sunil can you please be more specific .Maybe with a snippet code
    – naijacoder
    Oct 12, 2018 at 0:15
  • Hi Sunil, Any update on this?
    – naijacoder
    Nov 23, 2018 at 6:12
0

I don't know if it is still needed, but I solved the problem the following way:

function onQuerySucceeded() {

    var fail = false;
    var i = 0;
    var items = collListItem.get_data();
    for(; !fail && i < collListItem.get_count(); i++)
    {   
        var item = items[i]; 
        item.set_item("TestUnit", "TestUnit");
        item.update();

        if (i > 0 && i % 50 == 0){ // every 50 items
            clientContext.executeQueryAsync(function(){
                checkIfFinish(i, collListItem.get_count());
            }, function(sender, args){
                fail = true;
                if (console && console.error){
                    console.error(args);
                }
            });
        }
    }   

    if (!fail && i > 0 && i % 50 != 0){ // if there are items not committed
        clientContext.executeQueryAsync(function(){
            checkIfFinish(i, collListItem.get_count());
        }, failCallback);
    }
}

var checkIfFinish = function(counter, totalItems){
    if (counter == totalitems){
        // TODO when all items have been updated
    }
}

I wrote the code without testing it, there could be some typos, but the logic should be fine. The only problem is, if there is an error, you have inconsistent data, this mean some items are updated some are not. There is nothing like a rollback.
Hope it may help somebody.

P.S. Always load lists by guid, not by title:

var list = web.get_lists().getById('a9a7f119-9d25-4cdd-a59d-bd5f8ab58c85');
0

The difference between your code to approach of Bulk Update are some steps:

(1) Need to load the web in other variable;

(2) Need to add an array variable for bulk update;

Copying your coding and ajust these items, the code will be like this:

function updateListItems() {

    // Create an instance of the current context to return context information
    clientContext = new SP.ClientContext.get_current();
    var oWeb = clientContext.get_web();
    this.oList = oWeb.get_lists().getByTitle('Test');
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query></Query></View>');
    this.collListItem = oList.getItems(camlQuery);
    clientContext.load(oWeb); 
    clientContext.load(this.oList);
    clientContext.load(this.collListItem);

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));


}

function onQuerySucceeded() {
    var arrayItems = [];
    var listItemEnumerator = this.collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) 
    {   
        var item   = listItemEnumerator.get_current();
        var itemId = item.get_item("ID");
        var items  = oList.getItemById(itemId);
        items.set_item("TestUnit", "TestUnit");
        //Commits changed properties of the list item
        items.update();
        arrayItems.push(items);
        clientContext.load(arrayItems[arrayItems.length - 1]);
    }   

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onUpdateSucceeded), Function.createDelegate(this, this.onQueryFailed));

}

Vide reference: BULK UPDATE IN SHAREPOINT LIST ITEMS USING JSOM

Your Answer

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

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