1

Can someone please point me good url to paging sharepoint list using client side object model. I have this code using javascript but its giving error unexpected response from server

    context = new SP.ClientContext.get_current();

    web = context.get_web();
    list = web.get_lists().getByTitle(listName);
    camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<View>" +
        "<ViewFields>" +
        "<FieldRef Name='Id'/>" +
        "<FieldRef Name='Name'/>" +

        "</ViewFields>" +
        "<RowLimit>" + pageSize + "</RowLimit></View>");
    //camlQuery.set_viewXml("<View><RowLimit>" + pageSize + "</RowLimit></View>");

    spItems = list.getItems(camlQuery);
    context.load(spItems, 'Include(Name,Id)'); //Retrieves the properties of a client object from the server.

    context.executeQueryAsync(
        Function.createDelegate(this, success),
        Function.createDelegate(this, fail));
   }

So please would be nice if someone give me same approach which i can do in code behind.

2

I did a Google search on "sharepoint javascript csom paging". The first hit was Paging in SharePoint JavaScript Client Object Model

The article contains this code:

var listItems;                   // The list of retrieved items.
var query;                       // For paging, reuse the same query object.
var targetList;                  // The list from which to retrieve items.
var clientContext;

function runCode() {
    clientContext = new SP.ClientContext();
    targetList = clientContext.get_web().get_lists().getByTitle('Announcements');
    query = new SP.CamlQuery();
    //Specifying the RowLimit will determine how many items will be fetched in one call to the server.
    query.set_viewXml("<View><ViewFields><FieldRef Name='Title'/></ViewFields><RowLimit>2</RowLimit></View>");
    listItems = targetList.getItems(query);
    clientContext.load(listItems);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {
    var message = "Titles, two at a time:\n";
    var listEnumerator = listItems.getEnumerator();
    while (listEnumerator.moveNext()) {
        message += "\nTitle=" + listEnumerator.get_current().get_item("Title")
    }
    alert(message);
    //Gets the id of the last element from the returned collection along with the query.
    var position = listItems.get_listItemCollectionPosition();
    //Position will be null if all the items in the collection are fetched and there are no more items to be fetched.
    if (position != null) {
        //If more items are to be fetched, make a second call to the server and fetch the next group of items.
        query.set_listItemCollectionPosition(position);
        listItems = targetList.getItems(query);
        clientContext.load(listItems);
        //Call the same function recursively until all the items in the current criteria are fetched.
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }

}

function onQueryFailed(sender, args) {
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
  • Beat me to it! I actually fired up BIWUG CAML Designer and did it instead of lmgtfy – Eric Alexander Jun 1 '15 at 15:39
  • i want to write this code in code behind so this wont work – Happy Jun 1 '15 at 15:41
  • 1
    @Happy, th code snippet in your post is JSOM (client side code) and so is the code above – Robert Lindgren Jun 1 '15 at 16:00
  • you see that mine and yours code is same. I was using javascript only but i want to use same in my c# code – Happy Jun 1 '15 at 16:17
  • 2
    @Happy Google for "SharePoint ListItemCollectionPosition paging" and you'll find a C# code example – Rob Windsor Jun 1 '15 at 17:08

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.