简体   繁体   English

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

[英]How to programmatically set a user as the Site Collection Administrator in SharePoint

I am working on a SharePoint 2013 site. 我正在使用SharePoint 2013网站。 I'm trying to set a user as the Site Collection Administrator using the following code: 我正在尝试使用以下代码将用户设置为网站集管理员:

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

When I go to 'Site Settings' in my SharePoint 2013 site, and open the 'Site Collection Administrators' list, I do see that user listed as the Site Collection Administrator. 当我在SharePoint 2013网站中转到“网站设置”并打开“网站集管理员”列表时,我确实看到该用户被列为网站集管理员。

All seems good. 一切似乎都很好。 Now when I open this Site using the same username (who is just added this Site Collection Administrator list), I get message: 现在当我使用相同的用户名(刚刚添加此网站集管理员列表)打开本网站时,我收到消息:

Sorry, this site hasn't been shared with you. 抱歉,此网站尚未与您分享。

What other things do we need to do other than making the user as the 'Site Collection Administrator' any other settings am I missing here? 除了将用户作为“网站集管理员”之外,我还需要做什么其他任何其他设置?

I suspect that this might have to do with how user identities are stored in the SharePoint content database. 我怀疑这可能与用户身份如何存储在SharePoint内容数据库中有关。 Have you tried examining the existing users to see precisely what is stored in the username property? 您是否尝试检查现有用户以准确查看username属性中存储的内容?

This page on my blog may or may not be related, but it describes how Forms Based users were stored quite strangely in the SharePoint Object Model... 我博客上的这个页面可能相关也可能不相关,但它描述了基于表单的用户如何在SharePoint对象模型中存储得非常奇怪......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM