none
Client Object Model - Update Multiple List Items RRS feed

  • Question

  • I have a need to update multiple list items via CSOM. I found
    http://social.msdn.microsoft.com/Forums/en-US/17447ba4-8d59-48c4-b784-859dee38d366/client-object-model-ecma-update-list-items?forum=sharepointdevelopmentprevious

    and copied the code exactly at first with no success, no errors. I've tried to tweak it to get something to work and while the code below shows me all the alerts that the update happens, the list items themselves are not updated.

    Can anyone point out where my error is?

    var siteUrl = '/sites/PortalCollabPri/JMRTesting';
    
    function retrieveListItems() {
        var clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle('IT Training Waitlist');
    
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'Is_x0020_Eligible\'/><Value Type=\'Integer\'>1</Value></Eq></Where></Query><RowLimit>100</RowLimit></View>');
        this.collListItem = oList.getItems(camlQuery);
    
        clientContext.load(collListItem);
    
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }
    
    function onQuerySucceeded(sender, args) {
        var listItemInfo = '';
        var listItemEnumerator = collListItem.getEnumerator();
        var context = new SP.ClientContext(siteUrl);
    
        while (listItemEnumerator.moveNext()) {
            var oListItem = listItemEnumerator.get_current();
            listItemInfo += '\nID: ' + oListItem.get_id() + '\nTitle: ' + oListItem.get_item('Title');
            oListItem.set_item('Eligibility_x0020_Score', 100);
            alert('set eligibility score on ' + oListItem.get_id()); //SHOWS AS EXPECTED
            oListItem.update();
            alert('and updated'); //SHOWS AS EXPECTED
            context.executeQueryAsync(Function.createDelegate(this, this.on_myUpdate_Success), Function.createDelegate(this, this.on_myUpdate_Failure));
    
            function on_myUpdate_Success() {
                alert('updated OK'); //NEVER SHOWS
            }
    
            function on_myUpdate_Failure() {
                alert('Nope, not updated'); //NEVER SHOWS
            }
        }
    
        alert(listItemInfo.toString());
    }
    
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    
    retrieveListItems();


    - Thank you. Respectfully, Janette M. Ricker

    Wednesday, September 17, 2014 5:10 PM

Answers

  • Hi Janette,

    Please test the code below in your environment, it works as expected in my site:

    retrieveListItems();
    
    var clientContext;
    
    function retrieveListItems() 
    {
        clientContext = new SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle('List4');        
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml('<View></Query><RowLimit>100</RowLimit></View>');
        this.collListItem = oList.getItems(camlQuery);        
        clientContext.load(collListItem);        
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));    
    }
    function onQuerySucceeded(sender, args)
    {   
    	var listItemEnumerator = collListItem.getEnumerator();        
    	while (listItemEnumerator.moveNext())
    	{
    		console.log("asdf");
    		var oListItem = listItemEnumerator.get_current();
    		oListItem.set_item('NumColumn', 199);
    		oListItem.update();  
    		clientContext.executeQueryAsync(Function.createDelegate(this, this.on_myUpdate_Success), Function.createDelegate(this, this.on_myUpdate_Failure));	
    	}
    }
    
    function onQueryFailed(sender, args) {
    
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    
    function on_myUpdate_Success() {
    	console.log("Updated Successfully.");
    }
    function on_myUpdate_Failure() {
    	console.log("Error while updating");
    }

    Best regards,

    Patrick


    Patrick Liang
    TechNet Community Support

    • Marked as answer by JaeEmAre Friday, September 19, 2014 12:56 PM
    Friday, September 19, 2014 7:23 AM
    Moderator

All replies

  • hi

    first of all move on_myUpdate_Success and on_myUpdate_Failure functions declarations outside of the loop body on the same level where onQuerySucceeded and onQueryFailed are defined. If it won't help second thing which can be checked is that items in listItemEnumerator collection may not be updateable. Try to fetch list item in the loop body and then update it:

    while (listItemEnumerator.moveNext()) {
      var oListItem = listItemEnumerator.get_current();
      var newListItem = oList.getItemById(oListItem.get_id());
      newListItem.set_item('Eligibility_x0020_Score', 100);
      newListItem.update();
    
      context.executeQueryAsync(Function.createDelegate(this, this.on_myUpdate_Success), Function.createDelegate(this, this.on_myUpdate_Failure));
    }


    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Wednesday, September 17, 2014 8:00 PM
  • Hi Janette,

    Please test the code below in your environment, it works as expected in my site:

    retrieveListItems();
    
    var clientContext;
    
    function retrieveListItems() 
    {
        clientContext = new SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle('List4');        
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml('<View></Query><RowLimit>100</RowLimit></View>');
        this.collListItem = oList.getItems(camlQuery);        
        clientContext.load(collListItem);        
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));    
    }
    function onQuerySucceeded(sender, args)
    {   
    	var listItemEnumerator = collListItem.getEnumerator();        
    	while (listItemEnumerator.moveNext())
    	{
    		console.log("asdf");
    		var oListItem = listItemEnumerator.get_current();
    		oListItem.set_item('NumColumn', 199);
    		oListItem.update();  
    		clientContext.executeQueryAsync(Function.createDelegate(this, this.on_myUpdate_Success), Function.createDelegate(this, this.on_myUpdate_Failure));	
    	}
    }
    
    function onQueryFailed(sender, args) {
    
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    
    function on_myUpdate_Success() {
    	console.log("Updated Successfully.");
    }
    function on_myUpdate_Failure() {
    	console.log("Error while updating");
    }

    Best regards,

    Patrick


    Patrick Liang
    TechNet Community Support

    • Marked as answer by JaeEmAre Friday, September 19, 2014 12:56 PM
    Friday, September 19, 2014 7:23 AM
    Moderator
  • Thank you. Copying your exact code and changing the list/field names did indeed work. As soon as I put my original CAML query back in though it does not. I guess that is a limitation of CSOM?

    - Thank you. Respectfully, Janette M. Ricker

    Friday, September 19, 2014 1:06 PM
  • Hi Janette,

    A suggestion for you is that you can test the CAML query statement in CAML Designer, it is a nice tool for generating CAML query statement:

    http://www.camldesigner.com/?p=594

    After you got a correct CAML query statement, then you can manage to apply it to the script.  

    Best regards,

    Patrick


    Patrick Liang
    TechNet Community Support

    Monday, September 22, 2014 2:10 AM
    Moderator