0

I want to do paging on sharepoint list. I have below code which is giving error.

var customlist;
var allItems;
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var listName = "MyList";
var list = web.get_lists().getByTitle(listName);

var spItems,
    position = null,
    nextPagingInfo,
    previousPagingInfo,
    pageIndex = 1, // default page index value 
    pageSize = 4, // default page size value 
    list,
    camlQuery,
    sortColumn = "Name";
debugger;
camlQuery = new SP.CamlQuery();

camlQuery.set_listItemCollectionPosition(position);

camlQuery.set_viewXml("<View>" +
    "<ViewFields>" +
    "<FieldRef Name='Name'/>" +
    "<FieldRef Name='Email'>" +
    "</ViewFields>" +
    "<Query>" +
    "<OrderBy>" +
    "<FieldRef Name='" + sortColumn + "' Ascending='true' />" +
    "</OrderBy>" +
    "</Query>" +
    "<RowLimit>" + pageSize + "</RowLimit></View>");

spItems = list.getItems(camlQuery);


context.load(spItems, 'Include(Name,Age)');

context.executeQueryAsync(
    Function.createDelegate(this, function () {

        var enumerator = spItems.getEnumerator();

        while (enumerator.moveNext()) {


            var currentListItem = enumerator.get_current();

            var name = currentListItem.get_item('Name');

            var attachments = currentListItem.get_attachmentFiles();
            for (var j = 0; j < attachments.get_count() ; j++) {
                attachment = attachments.getItemAtIndex(j);
                console.log(attachment.get_serverRelativeUrl());


            }

        }
        div.html(innerHtml);

    }),
    Function.createDelegate(this, fail)
);

function fail() {
    debugger;
    alert(arguments[1].get_message());
}

Error is

value doesnt fall within expected range

I think error is coming when i write this line

 camlQuery.set_listItemCollectionPosition(position);

Not sure what to pass in position variable. When I load page for first time what should I pass in position variable?

Update1

If i remove code

camlQuery.set_viewXml("<View>" +
    "<ViewFields>" +
    "<FieldRef Name='Name'/>" +
    "<FieldRef Name='Email'/>" +
    "</ViewFields>" +
    "<Query>" +
    "<OrderBy>" +
    "<FieldRef Name='" + sortColumn + "' Ascending='true' />" +
    "</OrderBy>" +
    "</Query>" +
    "<RowLimit>" + pageSize + "</RowLimit></View>");

and pass hardcoded pagesize then i dont get error

camlQuery.set_viewXml('<View><RowLimit>3</RowLimit></View>');

Update2 Here is list view enter image description here

1

your original query has invalid xml, it should be

<FieldRef Name='Email' />

not

<FieldRef Name='Email'>
0

Declarations

var nextPagingInfo;
var previousPagingInfo;
var position;

Initial call you can make position null, so it starts from the begining

position = null;

//...CAML Query etc
context.load(spItems); 

Now you can invoke the Query using executeQueryAsync. And use below onSuccess

function onSuccess() {   
    var listEnumerator = spItems.getEnumerator(); 
    var items = []; 
    var item; 

    while (listEnumerator.moveNext()) { 
        //Do your logic
    } 
    if (spItems.get_listItemCollectionPosition()) { 
        nextPagingInfo = spItems.get_listItemCollectionPosition().get_pagingInfo(); 
    } else { 
        nextPagingInfo = null; 
    }        
} 

You can retrieve Next page items using

pageIndex++;
if (nextPagingInfo) { 
    position = new SP.ListItemCollectionPosition(); 
    position.set_pagingInfo(nextPagingInfo); 
} 

You can retrieve Previous page items using

pageIndex = pageIndex - 1; 
position = new SP.ListItemCollectionPosition(); 
position.set_pagingInfo(previousPagingInfo); 
  • i get error on line position.setpaginginfo(position) . error is Unable to get property 'set_pagingInfo' of undefined or null reference – DevelopmentIsMyPassion May 12 '15 at 18:34
  • Please see my updated question. It fails and go inside fail function. So i dont know what is wrong. I removed line position.setpaginginfo(position) – DevelopmentIsMyPassion May 12 '15 at 18:46
  • After line context.load(spItems, 'Include(Name,Age)'); it directly goes in function fail... – DevelopmentIsMyPassion May 12 '15 at 18:53
  • --------------------------- Message from webpage --------------------------- Failed to get items. Error:Value does not fall within the expected range. --------------------------- OK --------------------------- – DevelopmentIsMyPassion May 12 '15 at 19:19
  • Tried but still same issue. It doesnt come inside first statement var enumerator = spItems.getEnumerator();. It directly goes to fail function – DevelopmentIsMyPassion May 12 '15 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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