Thursday, August 29, 2013

Add Site Collection Administrators Programmatically 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"
 
 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



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...