Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

Add Site Collection Administrators Programmatically in SharePoint

$
0
0
SharePoint site collection administrators have Full rights to all sites within a site collection. Primary site collection administrator and Secodary site collection administrator specified in central administration will automatically become site collection admins. Its also possible to add multiple site collection admins for any SharePoint site collection.

How to add site collection administrator in SharePoint?
To add site collection administrator in SharePoint, you must be either Farm Administrator or another Site Collection Administrator of the site collection. Navigate to Site Settings >> Click on "Site collection administrators" link under "Users and Permissions"
add site collection administrator programmatically

which will lead you to a page where you can manage site collection administrators.

How to Add Site Collection Administrators Programmatically with C#?
In some scenarios, we may have to add site collection administrators programmatically. Say for e.g. There may be a requirement to add a particular user as site collection administrator to multiple site collections. Here is the code to add site collection administratorprogrammatically:
            //Define the parameter values: Site collection URL and user account to remove
            string siteURL = "http://sharepoint.crescent.com/sites/operations";
            string userAccount = @"global\Salaudeen";

            using(SPSite site=new SPSite(siteURL))
            {
                using (SPWeb web = site.RootWeb)
                  {
                    //Get the User 
                     SPUser user = web.EnsureUser(userAccount);

                    //Make the user as Site collection Admin
                     user.IsSiteAdmin = true;
                     user.Update();

                    //Print a message
                    Console.WriteLine("User: "+userAccount +" has been added as site collection administrator!");
                   }
            }

            //Pause
            Console.ReadLine();
       
Add site collection administrator SharePoint 2010 with PowerShell
The above code can be transformed to PowerShell to add site collection administrator SharePoint 2010 programmatically:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

    #Define the parameter values: Site collection URL and user account to remove
    $siteURL = "http://sharepoint.crescent.com/sites/operations"
    $userAccount = "Global\Salaudeen"
 
 Start-SPAssignment -Global
 
 #Get the RootWeb
 $web= Get-SPWeb $siteURL
 #Get the user acount - If doesn't exists ADD
 $user = $web.EnsureUser($userAccount)

 #Make the user as Site collection Admin
        $user.IsSiteAdmin = $true
        $user.Update()

       #Print a message
 Write-host "User: $($userAccount) has been added as site collection administrator!"
    
 Stop-SPAssignment -Global
So with the Power of scripting, we can add site collection administrator to all sites.

Add site collection administrator using stsadm:
We can also add a site collection admin for MOSS 2007 using STSADM tool. Here is an example:
stsadm.exe -o adduser -url "http://sharepoint.crescent.com/sites/operations" -userlogin "Global\Salaudeen" -useremail "Salaudeen@crescent.com" -role "Full Control" -username "Salaudeen Rajack" -siteadmin

If you are looking for a way to set SharePoint site collection's Primary administrator or secondary administrator, here is my another post for you: How to Change Site Collection Primary, Secondary Administrators in SharePoint

Viewing all articles
Browse latest Browse all 1058

Trending Articles