3
votes

I am working on a SharePoint 2013 site. 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.

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?

1
I don't have an answer to this question specifically, but after reviewing your profile I see that you have asked several questions and yet have not marked any answers. To help us help you, please consider going through your previous questions and marking answers that solved your problem.EtherDragon
if you add admin using powershell: Set-SPSite -Identity "<SiteCollection>" -SecondaryOwnerAlias "<User>", <User> is name of the user whom you want to add in the format <domain>\<username>. does it have the same issue?urlreader
Yes the powershell command does add a user as the 'secondaryowner' but not many, that means there can only be one secondaryowner. I like to add many users as the site collection administrator in SharePoint 2013.theITvideos

1 Answers

0
votes

I suspect that this might have to do with how user identities are stored in the SharePoint content database. Have you tried examining the existing users to see precisely what is stored in the username property?

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...