3

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?

Amir
  • 714
  • 1
  • 13
  • 37
theITvideos
  • 1,462
  • 2
  • 18
  • 29
  • 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 Dec 20 '12 at 23:15
  • if you add admin using powershell: Set-SPSite -Identity "" -SecondaryOwnerAlias "", is name of the user whom you want to add in the format \. does it have the same issue? – urlreader Dec 20 '12 at 23:48
  • 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 Dec 27 '12 at 00:12

1 Answers1

0

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

EtherDragon
  • 2,679
  • 1
  • 18
  • 24
  • whats the table name in the content database do we look for to find what is stored in the username property. thanks! – theITvideos Dec 20 '12 at 23:30
  • or perhaps is it something to do with Site Sharing? as the message says: 'Sorry, this site hasn't been shared with you' – theITvideos Dec 21 '12 at 00:11
  • You don't get any data directly from the SharePoint content database - the only way to find this out is through the SharePoint Object Model. – EtherDragon Dec 26 '12 at 17:23
  • the c# code written on the top works perfectly fine for SharePoint 2010 sites but not 2013. what other flag can we set to actually add user as the site collection administrator – theITvideos Dec 27 '12 at 00:15