How to use pagination to overcome SharePoint CSOM threshold error when working with a Folder object

61 views Asked by At

I'm using C# and SharePoint CSOM to retrieve data. I have a folder with more than 5000 files. When I'm trying to get all the files under this folder, I'm getting an exception:

Microsoft.SharePoint.Client.ServerException: The attempted operation is prohibited because it exceeds the list view threshold.

I know there is a way to use pagination with Lists but I'm working with Microsoft.SharePoint.Client.Folder:

var folderChildren = new List<ClientObject>();

var retrievals = new Expression<Func<Folder, object>>[shouldFetchFiles ? 2 : 1];
retrievals[0] = f => f.Folders;

if (shouldFetchFiles)
{
  retrievals[1] = f => f.Files.Include(
      file => file.UniqueId,
      file => file.ServerRelativeUrl,
      file => file.TimeLastModified,
      file => file.Length,
      file => file.ListItemAllFields.HasUniqueRoleAssignments
  );
}

// load sub-folders and files (if necessary)
lock (folder.Context)
{
  folder.Context.Load(folder, retrievals);
  folder.Context.ExecuteQuery();

  // add lists to children list
  folder.Folders.ToList().ForEach(f => folderChildren.Add(f));

  if (shouldFetchFiles)
  {
      foreach (var file in folder.Files)
      {
          if (fetchNonResources || fileHasUniquePermissions(file))
          {
              folderChildren.Add(file);
          }
      }
  }
}

Is there a way for my code to use pagination and overcome the 5000 items limit and the exceed threshold exception?

0

There are 0 answers