如何以编程方式将用户设置为SharePoint中的网站集管理员

时间:2012-12-20 22:55:15

标签: c# sharepoint administrator sharepoint-2013 sitecollection

我正在使用SharePoint 2013网站。我正在尝试使用以下代码将用户设置为网站集管理员:

public void SetUserSiteCollectionAdmin(string siteUrl, string strUserName, string strEmail, string fullName)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(siteUrl))
                    {
                       using (SPWeb web = site.RootWeb)
                         {
                           web.AllowUnsafeUpdates = true;
                           web.AllUsers.Add(strUserName, strEmail, fullName, null);
                           SPUser spUser = web.SiteUsers[strUserName];

                           spUser.IsSiteAdmin = true;

                           spUser.Update();

                           web.AllowUnsafeUpdates = false;
                         }
                    }
            });
}

当我在SharePoint 2013网站中转到“网站设置”并打开“网站集管理员”列表时,我执行会将该用户列为网站集管理员。

一切似乎都很好。现在当我使用相同的用户名(刚刚添加此网站集管理员列表)打开本网站时,我收到消息:

抱歉,此网站尚未与您分享。

除了让用户成为“网站集管理员”以外,我还需要做什么其他事情,我在这里缺少任何其他设置?

1 个答案:

答案 0 :(得分:0)

我怀疑这可能与用户身份如何存储在SharePoint内容数据库中有关。您是否尝试检查现有用户以准确查看用户名属性中存储的内容?

This page on my blog可能相关也可能不相关,但它描述了基于表单的用户如何在SharePoint对象模型中存储得非常奇怪......