Hi @Pablo Glomby,
Judging from the scenario and code you provided, the cause of the problem is:
- SharePoint limits the size of the query result set by default.
To solve this question, please try using pagination.
I made a test for your reference:
Folders containing more than five thousand files
Here is code:
static void Main(string[] args)
{
string libraryName = "Documents";
string folderUrl = "/sites/Fctest/Shared Documents/Fctest";
using (ClientContext context = new ClientContext(SiteURL))
{
....
List list = context.Web.Lists.GetByTitle(libraryName);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View Scope='RecursiveAll'><Query><OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy></Query><RowLimit Paged='TRUE'>500</RowLimit></View>";
context.Load(context.Web);
context.ExecuteQuery();
camlQuery.FolderServerRelativeUrl = $"{context.Web.ServerRelativeUrl}{folderUrl}";
ListItemCollectionPosition position = null;
//The code then uses a loop to iterate through the items in the library.
//Each iteration of the loop retrieves the next batch of items using the method with the object and the object, which indicates the position in the list or library where the previous batch of items ended.
do
{
camlQuery.ListItemCollectionPosition = position;
ListItemCollection items = list.GetItems(camlQuery);
context.Load(items);
context.ExecuteQuery();
position = items.ListItemCollectionPosition;
//The loop then iterates through the items in the current batch and performs any required operations, such as printing the file path.
foreach (var item in items)
{
Console.WriteLine(item["FileRef"]);
}
} while (position != null);
}
}
Here is test result:
Here is a link about how to paginate for your reference:
https://sharepoint.stackexchange.com/questions/128373/large-list-issue-with-csom
I hope this helps you solve your problem!
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards
Cheng Feng