SharePoint 2019:CSOM添加企业资源超时但资源仍创建问题
问题根源分析
- 初始查询一次性加载所有企业资源、自定义字段和查找表,数据量较大时会导致首次
ExecuteQuery()耗时过长触发超时。 - 添加资源后再次加载全部企业资源,属于无意义的冗余操作,进一步加重服务器负载。
- Project Server创建企业资源的底层操作是异步执行的,客户端超时后服务器仍会在后台完成创建,因此会出现“超时但资源最终存在”的矛盾现象。
具体解决方案
1. 缩小初始查询的加载范围
不要一次性加载所有资源和字段,仅加载当前操作必需的元数据(如果不需要自定义字段或查找表赋值,可直接省略相关加载逻辑)。
using (var Context = new ProjectContext(PWA_Url)) { // 仅在需要使用特定自定义字段/查找表时才加载,通过条件过滤减少数据量 // 不需要的话可直接删除这两行Load代码 Context.Load(Context.CustomFields, cf => cf.Where(f => f.InternalName == "目标字段内部名")); Context.Load(Context.LookupTables, lt => lt.Where(t => t.Name == "目标查找表名")); Context.ExecuteQuery(); // 此时查询数据量大幅降低,避免超时 // 创建资源的逻辑... }
2. 添加资源后避免重新加载全部资源
添加资源后无需加载所有企业资源,若需验证创建结果,仅加载刚创建的单个资源即可,或直接省略加载步骤。
var newResourceId = Guid.NewGuid(); var newResourceInfo = new EnterpriseResourceCreationInformation { Id = newResourceId, IsBudget = false, IsGeneric = false, IsInactive = false, Name = "Test_User", ResourceType = EnterpriseResourceType.Work }; // 添加资源并获取返回对象 var newResource = Context.EnterpriseResources.Add(newResourceInfo); Context.ExecuteQuery(); // 仅执行添加操作,不更新整个资源集合 // 如需验证,仅加载当前创建的资源 Context.Load(newResource); Context.ExecuteQuery();
3. 调整CSOM请求超时时间
针对批量操作,适当延长Context的超时时间,避免正常耗时的操作被误判为超时。
using (var Context = new ProjectContext(PWA_Url)) { // 设置超时时间为5分钟(单位:毫秒,可根据实际情况调整) Context.RequestTimeout = 300000; // 后续操作逻辑... }
4. 批量创建资源,减少请求次数
批量上传时,一次性添加所有资源信息后统一提交,大幅减少请求次数和服务器负载。
using (var Context = new ProjectContext(PWA_Url)) { Context.RequestTimeout = 300000; // 准备批量资源数据 var resourceBatch = new List<EnterpriseResourceCreationInformation> { new EnterpriseResourceCreationInformation { Id = Guid.NewGuid(), Name = "Test_User1", ResourceType = EnterpriseResourceType.Work, IsGeneric = false }, new EnterpriseResourceCreationInformation { Id = Guid.NewGuid(), Name = "Test_User2", ResourceType = EnterpriseResourceType.Work, IsGeneric = false } // 可添加更多资源... }; // 批量添加资源到上下文 foreach (var resourceInfo in resourceBatch) { Context.EnterpriseResources.Add(resourceInfo); } // 一次性提交所有创建请求 Context.ExecuteQuery(); }
额外建议
- 检查PWA服务器性能:若服务器CPU、内存占用过高,会直接导致请求超时,需监控服务器资源使用情况。
- 避开业务高峰期:选择低峰时段执行批量资源上传,降低服务器负载压力。
内容的提问来源于stack exchange,提问作者Abhishek Bandi
相关产品推荐
相关产品推荐

