0

I have the following method in place and trying figure out how to update for paginated results using int pageSize & int page parameters. Currently I have pageSize working using CamlQuery RowLimit, but not sure how to use page as there does not appear to be any built in methods:

public static List<SharePointCustomDocumentObj> GetDocumentsByFolderPath(string siteUrl, string folderPath, bool includeAllSubFolders = true, int pageSize = 10, int page = 1)
{
    List<SharePointCustomDocumentObj> docs = new List<SharePointCustomDocumentObj>();

    if (!string.IsNullOrWhiteSpace(siteUrl) && !string.IsNullOrWhiteSpace(folderPath))
    {
        try
        {
             
            // get site auth client context (gets disposded end of using statement)
            using (ClientContext spClientContext = new AuthenticationManager().GetACSAppOnlyContext(siteUrl, _clientId, _clientSecret))
            {
                if (spClientContext != null)
                {
                    // include sub-folders optional query scope
                    HtmlString recursiveAllAttribute = includeAllSubFolders ? new HtmlString("Scope=\"RecursiveAll\"") : new HtmlString(string.Empty);

                    CamlQuery camlQuery = new CamlQuery(); // init new caml query

                    camlQuery.DatesInUtc = false; // uses site regional time zone datetime when set to false

                    // caml query filter by folder path - format: {sites/sitename}/Shared Documents/{folderPath}
                    camlQuery.FolderServerRelativeUrl = GetSiteUrlAbsolutePath(siteUrl) + "/Shared Documents/" + folderPath;

                    // caml query files only, recursive, pagination statements
                    camlQuery.ViewXml = "<View " + recursiveAllAttribute + ">" +
                                            "<Query>" +
                                                "<Where>" +
                                                    "<Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq>" +
                                                "</Where>" +
                                            "</Query>" +
                                            "<RowLimit Paged=\"TRUE\">" + pageSize.ToString() + "</RowLimit>" +
                                        "</View>";

                    
                    List list = spClientContext.Web.Lists.GetByTitle("Documents"); // get root lists

                    ListItemCollection listItems = list.GetItems(camlQuery); // apply caml query to root lists

                    // load ListItem obj proprties
                    spClientContext.Load(listItems,
                            items => items.Include(
                                item => item.DisplayName,
                                item => item.FileSystemObjectType,
                                item => item.File,
                                item => item.File.UniqueId,
                                item => item.File.Name));

                    spClientContext.ExecuteQuery(); // execute query

                    if (listItems != null && listItems.Count > 0)
                    {
                        foreach (ListItem item in listItems)
                        {
                            // do stuff
                        }
                    }
                }
            }

        }
        catch (Exception ex)
        {
            // log (error) exception
        }
    }

    return docs;
}

In my view I would to call the method in following fashion (where pageSize and page would be passed via query strings on the page):

List<SharePointCustomDocumentObj> paginatedFiles = SharePointDocumentManager.GetDocumentsByFolderPath(spSiteUrl, "FolderPathName", true, 10, 1);

Return: Files 1-10

List<SharePointCustomDocumentObj> paginatedFiles = SharePointDocumentManager.GetDocumentsByFolderPath(spSiteUrl, "FolderPathName", true, 10, 2);

Return: Files 11-20

List<SharePointCustomDocumentObj> paginatedFiles = SharePointDocumentManager.GetDocumentsByFolderPath(spSiteUrl, "FolderPathName", true, 10, 3);

Return: Files 21-30

Thanks for any feedback!

0

Theres a SP.ListItemCollectionPagination class you can utilize, but it has it's limitations.

camlQuery.set_listItemCollectionPosition(position); 
var position = new SP.ListItemCollectionPosition(); 

position.set_pagingInfo(pageInfo);

where pageinfo can be derived from PagePrev=True&Paged=TRUE&p_ID=<firstrowItemID> or Paged=TRUE&p_ID=<lastrowItemID>

More info: https://social.technet.microsoft.com/wiki/contents/articles/18606.sharepoint-2013-paging-with-sharepoint-client-object-model.aspx

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.