1

I can get the 5 first items of a list with caml :

    var collListItemToBeUpdated = "";
    var listItemToBeUpdated = "";
    var enumerator;
    function getItemsToBeUpdated2() //this function called on button click to get ID's
    {
         var clientContext = new SP.ClientContext.get_current();
         var oList = clientContext.get_web().get_lists().getByTitle('Matériel Recyclage');
         var camlQuery = new SP.CamlQuery();
         camlQuery.set_viewXml("<View> <Query> <OrderBy> <FieldRef Name='ID' Ascending='True' /> </OrderBy> </Query> <RowLimit>10</RowLimit></View> ");
         collListItemToBeUpdated = oList.getItems(camlQuery);
         clientContext.load(collListItemToBeUpdated);

         clientContext.executeQueryAsync(getItemsToBeUpdatedSuccess2, getItemsToBeUpdatedFailed2);
    }
    function getItemsToBeUpdatedSuccess2()
    {
        var i = 0;
         enumerator = collListItemToBeUpdated.getEnumerator();
            while (enumerator.moveNext()) {
             if(i>4)
             {

                    var oItem = enumerator.get_current();
                    var oListItem = oList.getItemById(oItem.get_id());
                    oListItem.set_item('Palette', '2');  
                    oListItem.update();
                    itemArray.push(oListItem);
                    clientContext.load(itemArray[itemArray.length-1]);
             }
             i++;
clientContext.executeQueryAsync(updateMultipleListItemsSuccess2, updateMultipleListItemsFailed2);
           }
         //updateMultipleListItems2();
    }
    function getItemsToBeUpdatedFailed2(sender, args)
    {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

    function updateMultipleListItems2() //this function called on successfully getting ID's
    {   
        var itemArray = [];
        var clientContext = SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle('Matériel Recyclage');

         while(enumerator.moveNext())
         {
            var oItem = ListItemToBeUpdated.get_current();
            var oListItem = oList.getItemById(oItem.get_id());
            oListItem.set_item('Palette', '2');  
            oListItem.update();
            itemArray.push(oListItem);
            clientContext.load(itemArray[itemArray.length-1]);
         }  
        clientContext.executeQueryAsync(updateMultipleListItemsSuccess2, updateMultipleListItemsFailed2);
    }
    function updateMultipleListItemsSuccess2() 
    {    
        alert('Items Updated');
    }

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

But can you tell me how to get the five items after? I mean not 1, 2, 3, 4, 5 but 6, 7, 8, 9, 10. Something like RowBegin=5

I'm not looking for something based on ID, but based on the number of rows, something that can be used with my code.

Thanks

8

2 Answers 2

1

Try this if it works for you.

You can avoid first five items using below mentioned listitems.getEnumerator() code.

    function getItemsToBeUpdated() //this function called on button click to get ID's
        {
             var clientContext = new SP.ClientContext.get_current();
             var oList = clientContext.get_web().get_lists().getByTitle('Matériel Recyclage');
             var camlQuery = new SP.CamlQuery();
             camlQuery.set_viewXml("<View> <Query> <OrderBy> <FieldRef Name='ID' Ascending='True' /> </OrderBy> </Query> <RowLimit>5</RowLimit></View> ");

             collListItemToBeUpdated = oList.getItems(camlQuery);             
             clientContext.load(collListItemToBeUpdated);

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

        }

function onQuerySucceeded() {
              var i = 0;
               var enumerator = collListItemToBeUpdated .getEnumerator();
                while (enumerator.moveNext()) {
                 if(i>4)
                 {
                         var item = enumerator.get_current().get_fieldValues();
                        console.log(item);
                 }
                 i++;
               }
            }
10
  • have you tried it? is this worked for you?
    – Hardik
    Commented Apr 25, 2016 at 13:13
  • I'm trying to make it work
    – Jey10
    Commented Apr 25, 2016 at 13:15
  • Good.. Please update here as well after it.
    – Hardik
    Commented Apr 25, 2016 at 13:16
  • I've updated my question with your code added, and my whole code. But I have the following error with your part : The collection has not been initialized. It has not been requested or the request has not been executed
    – Jey10
    Commented Apr 25, 2016 at 13:23
  • Try this updated code.. & call failed method for as per your need.
    – Hardik
    Commented Apr 25, 2016 at 13:28
0

I would grab the first 10 items, store them in an array, then return the last 5 items from the array.

4
  • that's a good idea, but it would be great to give me a way to begin working with array as you recommend
    – Jey10
    Commented Apr 25, 2016 at 12:59
  • Actually, I think your enumeration with i>4 ought to do the trick if you change your CAML row limit to 10. Or you could use your itemArray.
    – Erin L
    Commented Apr 25, 2016 at 13:44
  • even with rowLimit=10, I have no error, but nothing is updated now. I think the "if" is close to do the work, but it doesn't place the item at the row 10
    – Jey10
    Commented Apr 25, 2016 at 13:51
  • To troubleshoot, will you try console.log(i) to make sure it's running all the way up to 9?
    – Erin L
    Commented Apr 25, 2016 at 14:00

Your Answer

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

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