None
None

Reputation: 5670

How to retrieve data from SharePoint API in paged manner (10 at a time)?

My code to retrieve all files inside a SharePoint list is like this

        CamlQuery camlQuery = new CamlQuery
        {
            ViewXml = @"<View Scope='Recursive'>
                             <Query>
                             </Query>
                         </View>",
            FolderServerRelativeUrl = myFolder.ServerRelativeUrl
        };
        ListItemCollection Items = list.GetItems(camlQuery);

This returns all the data inside a sharepoint list in a go, I want to retrieve only First 10 items (want to impliment paging), How can I achieve this?

Upvotes: 0

Views: 921

Answers (1)

Michael Han
Michael Han

Reputation: 3655

You could add RowLimit in the caml query, for example:

         Microsoft.SharePoint.Client.CamlQuery camlQuery = new CamlQuery();
         camlQuery.ViewXml =
            @"<View Scope='RecursiveAll'>
             <RowLimit Page='True' >5000</RowLimit>
             </View>";

         //Creating a single buffer for storing all the ListItems
         List<ListItem> lstListItemCollection = new List<ListItem>();

         do
         {
             ListItemCollection itemCollection = list.GetItems(camlQuery); 
             context.Load(itemCollection);
                
             try
             {
                 context.ExecuteQuery();
                 lstListItemCollection.AddRange(itemCollection);
                 camlQuery.ListItemCollectionPosition = itemCollection.ListItemCollectionPosition;

             }
             catch (Exception exec)
             {
                 Console.WriteLine(exec.ToString());
             }
         }
         while (camlQuery.ListItemCollectionPosition != null);

Reference: https://www.sptrenches.com/2016/06/get-all-items-in-5000-large-list-with.html

Upvotes: 1

Related Questions