Sharepoint客户端对象模型尚未初始化属性或字段

我有一个C#程序,使用Sharepoint客户端对象模型管理Sharepoint列表。 有时我们会遇到服务器问题,这会阻止程序访问sharepoint服务器。 我正在使用一个帮助程序类来运行ExecuteQuery方法并具有exception处理以继续执行,直到没有exception。

private void ExecuteContextQuery(ref ClientContext siteContext) { int timeOut = 10000; int numberOfConnectionErrors = 0; int maxNumberOfRetry = 60; while (numberOfConnectionErrors < maxNumberOfRetry) { try { siteContext.ExecuteQuery(); break; } catch (Exception Ex) { numberOfConnectionErrors++; Service.applicationLog.WriteLine("Unable to connect to the sharepoint site. Retrying in " + timeOut); Service.applicationLog.WriteLine("Exception " + Ex.Message + " " + Ex.StackTrace); System.Threading.Thread.Sleep(timeOut); if (numberOfConnectionErrors == maxNumberOfRetry) { throw Ex; } } } } 

但是我收到错误消息

属性或字段“LoginName”尚未初始化。

集合尚未初始化。 尚未请求或请求尚未执行。 可能需要明确请求。

错误消息似乎与我调用Load方法的方法有关。 这是我调用上述方法的代码示例。

  List sharepointList = siteContext.Web.Lists.GetByTitle(this._listName); CamlQuery query = CamlQuery.CreateAllItemsQuery(); items = sharepointList.GetItems(query); siteContext.Load(items); //siteContext.ExecuteQuery(); ExecuteContextQuery(ref siteContext); 

每次调用ExecuteQuery时是否需要重新加载站点上下文? 这就是为什么我看到上面的错误消息?

这是我用来获取生成错误的登录ID的函数

  public String getLoginIDbyUserId(int userID) { ClientContext siteContext = getClientContextObject(); User _getUser = siteContext.Web.SiteUsers.GetById(userID); siteContext.Load(_getUser); //siteContext.ExecuteQuery(); ExecuteContextQuery(ref siteContext); String loginID = String.Empty; String formatedLoginID = String.Empty; loginID = _getUser.LoginName; if (loginID.Contains('|')) { formatedLoginID = loginID.Substring(loginID.IndexOf('|') + 1); } siteContext.Dispose(); return formatedLoginID; } 

请在加载用户对象时尝试加载用户的LoginName属性。 在excutequery方法之后,尝试使用User的 LoginName属性

 siteContext.Load(_getUser, u => u.LoginName); 

在此更改后,您的代码应如下所示

 public String getLoginIDbyUserId(int userID) { ClientContext siteContext = getClientContextObject(); User _getUser = siteContext.Web.SiteUsers.GetById(userID); siteContext.Load(_getUser, u => u.LoginName); //siteContext.ExecuteQuery(); ExecuteContextQuery(ref siteContext); String loginID = String.Empty; String formatedLoginID = String.Empty; loginID = _getUser.LoginName; if (loginID.Contains('|')) { formatedLoginID = loginID.Substring(loginID.IndexOf('|') + 1); } siteContext.Dispose(); return formatedLoginID; }