Brijesh's Blog

April 16, 2008

How to add another site collection administrator to all the site collections in SharePoint server farm?

Filed under: SharePoint 2007 — brij28 @ 8:15 pm

First of all, let me make this clear that I am not a good SharePoint Developer but I have tried my best to achieve this programmatically.

Few days back, we decided to outsource SharePoint support. The new support professionals will be responsible for troubleshooting basic SharePoint issues and if require, they should be able to provide the end user with the contact information of particular site collection administrator.

Basic SharePoint issues such as – Request for creating a new sub site under particular site collection(Top Level Site or Departmental Site), Unable to access SharePoint, Unable to edit SharePoint contents, Unable to access lists, libraries etc.

We have site collection created for each department and each site collection has one or more site collection administrators, who are responsible for managing the site collection, they belong to. If site collection administrators are unable to address the end user’s issue, the request will be forwarded to SharePoint Farm Administrators – I am the part of that team.

Now as we decided to outsource SharePoint support, we had following requirements:

1. Create a new AD account, which will be used by all the support professionals to access any site with "Administrator" privileges in the SharePoint server farm. Lets say this account is "domain\spsupport"
2. If any support professional is unable to resolve the issue and he/she wants to direct end user’s request to the site collection administrator, he/she should be able to access any site collection with "Administrator" privileges to provide name and contact information of site collection administrator to the end user. Support professional should use "domain\spsupport" account for this purpose. They can also use this account to find out the content owner information and forward permissions requests/issues to the content owner – e.g. If somebody needs access to a specific document library of a site or when somebody request specific permissions to lists/libraries of a site.
3. Finally make "domain\spsupport" account as site collection administrator for all the site collections in the SharePoint server farm.

We have almost 300 site collections and adding "domain\spsupport" account as site collection administrator to each site collection using Central Administration site or Site Settings does not seem to be an appropriate solution. So I decided to write a simple C# application which will output stsadm commands for adding "domain\spsupport" account as site collection administrator to each site collection in the farm.



Here is the code,

Note:

1. This code assumes that you have all the site collections created under "sites/"
2. This is a console application so you have to use it the way you use stsadm command.
3. This code will output stsadm commands. You may run it as follows to generate batch file for all the stsadm commands,

AddSCAdmin.exe -url http://sharepoint.domain.com -user domain\spsupport > addSCAdmin.bat

4. Finally you can run addSCAdmin.bat file, which will execute all the stsadm commands for adding domain\spsupport account as site collection administrator.

5. addSCAdmin.bat file should look like this,

stsadm -o siteowner -url http://sharepoint.domain.com/sites/SC1 -secondarylogin domain\spsupport
stsadm -o siteowner -url http://sharepoint.domain.com/sites/SC2 -secondarylogin domain\spsupport
stsadm -o siteowner -url http://sharepoint.domain.com/sites/SC3 -secondarylogin domain\spsupport
stsadm -o siteowner -url http://sharepoint.domain.com/sites/ITSiteCollection -secondarylogin domain\spsupport
stsadm -o siteowner -url http://sharepoint.domain.com/sites/MktSC -secondarylogin domain\spsupport
………………………….
……………….
……..


Disclaimer: Please do not use this code unless you have tested it in test environment first. I will not be responsible for the outcome of this code.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.StsAdmin;

namespace AddSCAdmin
{
    class AddSCAdminClass
    {              
        static void Main(string[] args)
        {
            AddSCAdminClass sc = new AddSCAdminClass();
            sc.addSiteCollectionAdmin(args);
        }

        private void addSiteCollectionAdmin(string[] args)
        {
            try
            {
                string mode = "";
                string virtualserver = "";
                string user = "";

                // get command line arguments
                if ((args.Length == 0) || (args.Length == 1 && (args[0] == "-?" || args[0] == "-help")))
                {
                    displayOutput("", mode);
                    displayOutput("AddSCAdmin.exe -url VirtualServerURL -user domainuser [-quiet]", mode);
                    displayOutput("", mode);
                    displayOutput("  -url VirtualServerURL: Full URL of virtual server starting with http://", mode);
                    return;
                }

                // loop through command line arguments
                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i] == "-url")
                    {
                        virtualserver = args[i+1];
                        i++;
                        // make sure url parameter starts with http:
                        if (!virtualserver.StartsWith("http:"))
                        {
                            throw new ArgumentOutOfRangeException("url parameter should start with http://&quot;);
                        }
                    }
                    else if (args[i] == "-user")
                    {
                        user = args[i + 1];
                        i++;
                    }
                      
                    else if (args[i] == "-quiet")
                    {
                        mode = "quiet";
                        continue;
                    }
                    else
                    {
                        displayOutput(String.Format("Unrecognized switch {0}", args[i]),mode);
                        return;
                    }
                }

                // connect to web application on virtualserver
                SPWebApplication webApp = SPWebApplication.Lookup(new Uri(virtualserver));
                // create site collections object for web application
                SPSiteCollection siteCollections = webApp.Sites;

                string SCUrl = "";
                int SCCount = 0;
                // loop through site collections
                foreach (SPSite siteCollection in siteCollections)
                {
                    //site = new SPSite(siteCollection.Url);
                    SCUrl = siteCollection.Url;
                    if (SCUrl.Contains("sites"))
                    {
                        SCCount++;
                        Console.WriteLine("stsadm -o siteowner -url " + siteCollection.Url +" -secondarylogin "+user);
                    }
                }
                //Console.WriteLine("Total Site Collection: " + SCCount);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        private void displayOutput(string text, string mode)
        {
            if (mode != "quiet")
            {
                Console.WriteLine(text);
            }
        }
    }
}


I have created C# project for this solution. Please send me an email if you want the zip file for the whole project.

I hope this will help few people for such specific need.

1 Comment »

  1. Really nice article and like to have the C# project zip file by email.You can send me at pravinu@hotmail.comThanks,Pravin

    Comment by Pravin — October 29, 2009 @ 7:16 am


RSS feed for comments on this post. TrackBack URI

Leave a comment

Create a free website or blog at WordPress.com.