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

Sharepoint Client Object Model The property or field has not been initialized

我有一个使用 Sharepoint 客户端对象模型管理 Sharepoint 列表的 C# 程序。有时我们会遇到服务器问题,这将阻止程序访问共享点服务器。我正在使用 class 到 运行 ExecuteQuery 方法的助手,并有异常处理继续执行,直到没有异常。

       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;
                    }

                }
         }

    }

但是我收到一条错误消息

The property or field 'LoginName' has not been initialized.

collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

错误消息似乎与我调用 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 方法之后,尝试使用 LoginName 属性 of User

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;
    }