首页 > 解决方案 > 无法从空对象调用方法或检索属性。以下调用堆栈返回的对象为空

问题描述

我正在使用 CSOM 和 azure 函数来创建网站集。我的工作流程首先使用 GetAzureADAppOnlyAuthenticatedContext 在创建网站后获取管理员客户端上下文。获取网站集客户端上下文,然后设置网站属性,例如将用户添加到组、网站所有者等。在本地调试时效果很好,但有时会出现以下错误:

Cannot invoke method or retrieve property from null object. Object returned by the following call stack is null. \"AssociatedOwnerGroup\r\nRootWeb\r\nSite\r\nMicrosoft.SharePoint.SPContext.Current\r\n\""

我的代码是这样的:

public ClientContext GetAzureADOnlyClientContext(string SiteUrl, string appId, string tenant, X509Certificate2 certificate,bool isadmin)
        {
            ClientContext newClientContext;
            try
            {
                newClientContext = new AuthenticationManager().GetAzureADAppOnlyAuthenticatedContext(SiteUrl, appId, tenant, certificate);
                if (!isadmin)
                {
                    Web web = newClientContext.Web;
                    newClientContext.Load(web, w => w.Url);
                }
                newClientContext.ExecuteQuery();
                return newClientContext;
            }
            catch (Exception ex)
            {
                newClientContext = null;
                if (_logHelper != null)
                {
                    _logHelper.writeLog("GetAzureADContextError:"+ex.Message, TraceLevel.Error, ex);
                }
                return null;
            }
        }

在主函数中

 while (ctxNew == null && count <= Constants.NEWSITE_TRYCOUNT)
                    {
                        logHelper.writeLog(string.Format("Site is being provisioned, waiting for {0} seconds ({1})", Constants.NEWSITE_SLEEP_SECONDS, count));
                        Thread.Sleep((int)Constants.NEWSITE_SLEEP_SECONDS * 2000);
                        //ctxNew = spHelper.GetClientContextByCredential(cred, true);
                        ctxNew = spHelper.GetAzureADOnlyClientContext(hostUrl, spAzureAppId, spTenant, certificate,false);

                        count++;
                    }

                    if (ctxNew == null)
                    {
                        logHelper.writeLog("New site collection could not be retrieved from " + hostUrl);
                    }
                    else
                    {
                        logHelper.writeLog("New site collection is created.");
                        Thread.Sleep((int)Constants.NEWSITE_SLEEP_SECONDS * 1000);
                        processRequestHelper = new ProcessRequestHelper(admClientContext, ctxNew, tenant, siteCreationInfo, log);
                        processRequestHelper.UpdateSite();
                        logHelper.writeLog(hostUrl + " has been updated.");
                    }



public void UpdateSite()
        {
            _logHelper.writeLog("Updating " + _newClientContext.Url);

            string description = _siteProperties.Description;
            string[] siteOwners = _siteProperties.BusinessOwnerEmail.Split(';');
            string[] members = _siteProperties.MemberEmails.ToArray();

            _tenant.SetSiteAdmin(_newClientContext.Url, _siteProperties.TechnicalOwnerEmail, true);
            _adminClientContext.ExecuteQuery();

            if (siteOwners.Length > 0)
            {
                AddGroupUser(_newClientContext.Site.RootWeb.AssociatedOwnerGroup, siteOwners);
            }

            if (members.Length > 0)
            {
                AddGroupUser(_newClientContext.Site.RootWeb.AssociatedMemberGroup, members);
            }

            if (!string.IsNullOrWhiteSpace(description))
            {
                _newClientContext.Site.RootWeb.Description = description;
                _newClientContext.Site.RootWeb.Update();
            }

            try
            {
                if (_newClientContext.HasPendingRequest)
                {
                    _newClientContext.ExecuteQuery();
                }

                _logHelper.writeLog("Site updated!");
            }
            catch (Exception ex)
            {
                _logHelper.writeLog("Update site error:"+ex.Message, TraceLevel.Error, ex);
                throw;
            }
        }

 private void AddGroupUser(Group grp, string[] usernameArr)
        {
            foreach (string username in usernameArr)
            {
                try
                {
                    _logHelper.writeLog("Add User " + username + " to group.");
                    User user = _newClientContext.Web.EnsureUser(username);
                    _newClientContext.Load(user);
                    grp.Users.AddUser(user);
                    _newClientContext.ExecuteQuery();
                }
                catch (Exception ex)
                {
                    _logHelper.writeLog("Add User " + username + ": " + ex.Message, TraceLevel.Error, ex);
                }
            }
        }

似乎有时 clientconetxt 在 azure 函数中变为空

标签: c#azure-functionssharepoint-onlinecsom

解决方案


推荐阅读