While trying to load all the files in a particular folder for processing, we got the below error
The attempted operation is prohibited because it exceeds the list view threshold – Microsoft.SharePoint.SPQueryThrottledException
The folder we were targeting contained 25K files in it. (the default threshold – 5000 items)

So we updated our code to make use of CamlQuery to retrieve the files in batch along with the listitemcollectionpostion object to loop and process through all the files.
Below is the sample code for reference –
var applicationId = "d7eaeeb7-ef0a-474d-9b94-567013576c14";
var password = "password";
var domain = "xyz.onmicrosoft.com";
var siteUrl = "https://xyz.sharepoint.com/sites/MyTeamSite";
var certPath = @"C:\SharePointApp\MyTestCertificate.pfx";
var folderRelativeUrl = "xyz/correspondences";
var authManager = new AuthenticationManager(applicationId, certPath, password, domain);
ClientContext clientContext = authManager.GetContext(siteUrl);
var folder = clientContext.Web.GetFolderByServerRelativeUrl(folderRelativeUrl);
clientContext.Load(folder);
// replaced it with usage of CamlQuery
//context.Load(folder.Files);
//context.ExecuteQuery();
ListItemCollectionPosition position = null;
do
{
CamlQuery camlQuery = new CamlQuery
{
ViewXml = $@"<View Scope='RecursiveAll'>
<Query>
<OrderBy>
<FieldRef Name='ID' Ascending='TRUE'/>
</OrderBy>
</Query>
<RowLimit>500</RowLimit>
</View>",
ListItemCollectionPosition = position
};
var listItems = folder.ListItemAllFields.ParentList.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
position = listItems.ListItemCollectionPosition;
foreach (ListItem listItem in listItems)
{
// To process only the files (not folder)
if (listItem.FileSystemObjectType == FileSystemObjectType.File)
{
var file = listItem.File;
clientContext.Load(file);
clientContext.ExecuteQuery();
ProcessFiles(file);
}
}
} while (position != null);
Also refer –
Hope it helps..
Discover more from Nishant Rana's Weblog
Subscribe to get the latest posts sent to your email.

One thought on “Fixed – The attempted operation is prohibited because it exceeds the list view threshold error– SharePoint Online”