Azure Web App(C#)访问SharePoint文档库触发「服务器负载过高任务被禁止」错误的解决求助
Hey there, let's break down why you're hitting this frustrating error and fix it step by step. First off, your suspicion about fetching folders instead of actual files is totally valid—folders count as list items in SharePoint, and if your document library has a deep folder structure, even a small row limit might force SharePoint to scan through thousands of hidden folder items under the hood. Let's start with that key fix.
1. Filter Out Folders in Your CAML Query
SharePoint uses the FSObjType field to distinguish files (value 0) from folders (value 1). Your current query doesn't exclude folders, so SharePoint is processing all folder items alongside your target files. Update your CAML to add a filter for only files:
CamlQuery cquery = new CamlQuery(); cquery.ViewXml = @"<View Scope='RecursiveAll'> <Query> <Where> <Eq> <FieldRef Name='FSObjType' /> <Value Type='Integer'>0</Value> </Eq> </Where> <OrderBy Override='TRUE'> <FieldRef Name='Title' Ascending='TRUE' /> </OrderBy> </Query> <RowLimit Paged='TRUE'>4</RowLimit> </View>";
This will immediately cut down the number of items SharePoint needs to process, which should directly alleviate the load issue.
2. Double-Check Your Index Setup
You mentioned creating an index for the Title column, but let's confirm a few critical details:
- Verify the index was created successfully (navigate to your document library settings → Indexed Columns, make sure
Titleis listed). - Ensure there are no other indexed columns conflicting with your
OrderByclause. SharePoint prioritizes indexed columns for sorting, but hidden view settings can sometimes override this and trigger a full scan.
3. Simplify Context Loading for Efficiency
Your current code uses LoadQuery to fetch the list, which works, but let's simplify it to avoid any potential context overhead. You can directly load the list by title instead:
Web rootWeb = context.Web; List list = rootWeb.Lists.GetByTitle("Documents"); context.Load(list); context.ExecuteQuery();
This is more straightforward and reduces the chance of unintended query behavior.
4. Verify Paging Behavior
Even with RowLimit Paged='TRUE', SharePoint might still process more items than expected if paging isn't working correctly. After adding the folder filter, test the query again. If the error persists, try removing the Override='TRUE' from the OrderBy clause—sometimes this forces SharePoint to ignore the indexed column and perform a full scan.
Why the PowerShell Script Works
Chances are the working PowerShell script explicitly filters out folders or uses a query scope that avoids scanning unnecessary items. Comparing your C# CAML to the PowerShell script's query would confirm this, but the folder filter is the most likely fix for your issue.
Start with adding the FSObjType filter first—it's the most impactful change here. Let me know if you still run into trouble after testing this!
内容的提问来源于stack exchange,提问作者YosiPel

