kwizcom banner advertisement

Add Site Collection Administrators Programmatically/PowerShell in SharePoint

SharePoint site collection administrators have Full rights to all sites within a site collection. Primary site collection administrator and Secondary 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 administrator programmatically:
            //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. Here is how to add site collection administrator in SharePoint 2010 using PowerShell:
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"
 
 #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!"  
 
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
Add Site Collection Administrators Programmatically/PowerShell in SharePoint Add Site Collection Administrators Programmatically/PowerShell in SharePoint Reviewed by Salaudeen Rajack on 11:34 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.