2

Paging is not working in here, the error it says : The query expression is not supported. Error occurs in this line:
clientContext.Load(listItems, itms => itms.Skip((PageNumber - 1) * PageSize).Take(PageSize));

Could someone advise Please?

Thanks

    /// <summary>
    /// Method to return list of documents of a specific document library
    /// </summary>
    /// <param name="docLibaryName"></param>
    /// <returns></returns>
    public List<Document> GetDocumentsByLibraryName(string spURL, string docLibaryName, int PageSize, int PageNumber)
    {

        List<Document> docList = new List<Document>();
        //Access the Document Library
        ClientContext clientContext = new ClientContext(spURL);
        List sharedDocumentsList = clientContext.Web.Lists.GetByTitle(docLibaryName);

        //Specify the Caml Query
        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ViewXml =
            @"<View Scope='Recursive'></View>";
        ListItemCollection listItems = sharedDocumentsList.GetItems(camlQuery);
         clientContext.Load(listItems, itms => itms.Skip((PageNumber - 1) * PageSize).Take(PageSize));
        clientContext.ExecuteQuery();
        AddItemsToDocumentCollection(docList, listItems);
        return docList.ToList();
    }
Hari Gillala
  • 11,736
  • 18
  • 70
  • 117

1 Answers1

2

The Skip operator is not supported, as the error indicates.

There is no Skip operator in CAML, so there is no way for the query provider to translate that query for you.

The only paging operation supported by Sharepoint is getting the next page, not the n-th page. You can get the next page by setting the CamlQuery object's ListItemCollectionPosition to be the ListItemCollectionPosition value of the ListItemCollection after the query is executed. If it's null, then there is no next page. You also need to set the RowLimit in the query's viewXML to determine the page size.

It'll look something like this:

using (var context = new ClientContext("http://vddp23q-5d16d1e/"))
{
    var query = new CamlQuery();
    var list = context.Web.Lists.GetByTitle("Documents");
    int pageSize = 10;
    query.ViewXml = string.Format(
@"<View><RowLimit Paged='true'>{0}</RowLimit></View>", pageSize);

    ListItemCollection items;
    do
    {
        items = list.GetItems(query);
        context.Load(items);
        context.ExecuteQuery();
        foreach (var item in items)
            DoStuff(item);
        query.ListItemCollectionPosition = items.ListItemCollectionPosition;
    }
    while (items.ListItemCollectionPosition != null);
}

Side note: make sure you dispose of your ClientContext objects.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • How could Paging can be done, using this approach? Do I need to look at some other approaches? – Hari Gillala Dec 23 '13 at 15:45
  • If it's null, then there is no next page. Thanks that's a good tip as going back after the end of the page is well impossible. At least it seems to be. – Richard Housham Nov 16 '17 at 14:25