1
votes

Trying to figure out how to update the following code to get paginated results by passing 'page' and 'pageSize':

using (ClientContext spClientContext = new AuthenticationManager().GetACSAppOnlyContext(siteUrl, _clientId, _clientSecret))
{
    if (spClientContext != null)
    {
        CamlQuery camlQuery = new CamlQuery();

        camlQuery.ViewXml = "<View Scope="RecursiveAll">" +
            "<Query>" +
            "<Where>" +
            "<Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq>" +
            "</Where>" +
            "<OrderBy><FieldRef Name=\"FileLeafRef\" Ascending=\"FALSE\"/></OrderBy>" +
            "</Query>" +
            "<RowLimit>10</RowLimit>" +
            "</View>";

        List list = spClientContext.Web.Lists.GetByTitle("Documents");

        ListItemCollection listItems = list.GetItems(camlQuery);

        spClientContext.Load(listItems,
                             items => items.Include(
                                 item => item.DisplayName,
                                 item => item.FileSystemObjectType,
                                 item => item.File,
                                 item => item.File.Name,
                                 item => item.File.Author,
                                 item => item.File.ModifiedBy,
                                 item => item.File.ListItemAllFields["Created"],
                                 item => item.File.ListItemAllFields["Modified"],
                                 item => item.File.ListItemAllFields["FileRef"],
                                 item => item.File.Length));

        spClientContext.ExecuteQuery();

        if (listItems != null && listItems.Count > 0)
        {
            foreach (ListItem item in listItems)
            {
                if (item.FileSystemObjectType.Equals(FileSystemObjectType.File))
                {
                    // Do Stuff
                }
            }
        }
    }
}

The code above would be included in a method where 'pageSize' would get passed to the CAML Query 'RowLimit' element value. That is working. I am fairly new to SharePoint CSOM dev so trying to figure out now how to set page or position so I can query only paginated results on request? Thanks for the help!

1

1 Answers

0
votes

The RowLimit property contains a boolean attribute named Paged, where the default value is FALSE. At RowLimit element (List) contains some specifications about this; but, this is a non-trivial implementation, probably you need to use do{...}while{...} looping to auxiliary your task, the final code will be similar example bellow:

    camlQuery.ViewXml = "<View Scope="RecursiveAll">" +
    "<Query>" +
    "<Where>" +
    "<Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq>" +
    "</Where>" +
    "<OrderBy><FieldRef Name=\"FileLeafRef\" Ascending=\"FALSE\"/></OrderBy>" +
    "</Query>" +
    "<RowLimit Paged='TRUE'>10</RowLimit>" +
    "</View>";

    List list = spClientContext.Web.Lists.GetByTitle("Documents");

    List<ListItem> listItemColl = new List<ListItem>();

    do
    {
     ListItemCollection listItems = list.GetItems(camlQuery);
     spClientContext.Load(listItems,
                     items => items.Include(
                         item => item.DisplayName,
                         item => item.FileSystemObjectType,
                         item => item.File,
                         item => item.File.Name,
                         item => item.File.Author,
                         item => item.File.ModifiedBy,
                         item => item.File.ListItemAllFields["Created"],
                         item => item.File.ListItemAllFields["Modified"],
                         item => item.File.ListItemAllFields["FileRef"],
                         item => item.File.Length));

     spClientContext.ExecuteQuery();
     context.Load(listItems);
        
         try
         {
         spClientContext.ExecuteQuery();
         listItemColl.AddRange(listItems);
         camlQuery.ListItemCollectionPosition = itemCollection.ListItemCollectionPosition;

         }
         catch (Exception ex)
         {
            throw new InvalidOperationException("Error on paging", ex);
         }
 }
 while (camlQuery.ListItemCollectionPosition != null);

If possible, give us feedback about your test.