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

≡放荡痞女 提交于 2019-12-23 04:41:20

问题


I have a C# program that manages Sharepoint lists using the Sharepoint Client Object Model. Occasionally we will have server issues which will prevent the program from accessing the sharepoint server. I am using a helper class to run the ExecuteQuery method and have exception handling to continue to execute until there is no 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;
                    }

                }
         }

    }

However I getting an error messages

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

and

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

The error messages seem to be related to methods where I call the Load method. here is an example of my code that calls the method above.

                List sharepointList = siteContext.Web.Lists.GetByTitle(this._listName);
            CamlQuery query = CamlQuery.CreateAllItemsQuery();
            items = sharepointList.GetItems(query);

            siteContext.Load(items);

            //siteContext.ExecuteQuery();
            ExecuteContextQuery(ref siteContext);

Do I need to reload the site context with every call to ExecuteQuery? Is that why I am seeing the error message above?

Here is the function I am using for getting the Login ID which is generating the error

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

回答1:


Please try to load LoginName property of user while loading user object. And after excutequery method, try to consume LoginName property of User

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

And after this change your code should look like this

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


来源:https://stackoverflow.com/questions/32830230/sharepoint-client-object-model-the-property-or-field-has-not-been-initialized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!