Brijesh's Blog

April 17, 2008

Custom code to get the list of site collection administrators for all the site collections in SharePoint server farm

Filed under: SharePoint 2007 — brij28 @ 7:37 pm
we have changed our idea of giving "Administrator" level access to all
the site collections to the outsourced team. Instead we decided to
deliver them an Excel document with the URL and site collection
administrators for each site collection. In fact, all the outsourced
team members have access to their own team site. So we are going to
upload this Excel document to their team site.

As usual, I wrote C# console application to achieve this.


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 a text file with comma separated values when you run it as follows,

SCAdmin.exe -url > SCAdmins.txt

4. Finally you can import SCAdmin.txt file to an Excel document with ","(comma) selected as separator for creating a new column.

5. SCAdmin.txt file should look like this,, domain\spadmin;, domain\spadmin; domain\user2; domain\user3, domain\user4; domain\user5;, domain\spadmin; domain\user6, domain\user7;

When you import SCAdmin.txt to an Excel document by selecting ","
(comma) as the separator, it will place URL in first column and site
collection administrators in second column.

Disclaimer: Please test this code in test environment before you use it. I will not be responsible the outcome of this code.

Here is the 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 SCAdmins
    class SCAdminsClass
        static void Main(string[] args)
            SCAdminsClass sc = new SCAdminsClass();

        private void getSiteCollectionAdministrators(string[] args)
                string mode = "";
                string virtualserver = "";

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

                // loop through command line arguments
                for (int i = 0; i < args.Length; i++)
                    if (args[i] == "-url")
                        virtualserver = args[i + 1];
                        // 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] == "-quiet")
                        mode = "quiet";
                        displayOutput(String.Format("Unrecognized switch {0}", args[i]), mode);

                // 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;
                SPSite site = null;
                SPWeb web = null;
                string admins = "";
                // loop through site collections
                foreach (SPSite siteCollection in siteCollections)
                    site = new SPSite(siteCollection.Url);
                    web = site.OpenWeb();
                    SCUrl = siteCollection.Url;
                    if (SCUrl.Contains("sites"))
                        SPUserCollection users = web.Users;
                        foreach (SPUser user in users)
                            if (user.IsSiteAdmin)
                                admins = admins+user.LoginName+"; ";
                        Console.WriteLine(SCUrl + ", " + admins);
                    admins = "";
                //Console.WriteLine("Total Site Collection: " + SCCount);
            catch (Exception e)

        private void displayOutput(string text, string mode)
            if (mode != "quiet")

Please send me an email if you need a zip file for this C# project. Enjoy!



  1. Yes! With a little modification on this code, I beleive this should great managing/ editing permissions of "My Site" sites. Can I please get the zip files for this project and/ or the post after "adding site collection administrator to all site collections…"? Thanks Brij 🙂

    Comment by Vasil — June 9, 2008 @ 5:28 pm

  2. That\’s great. Could you send me the zip file please? Thanks!!.

    Comment by Gianpaolo — May 6, 2009 @ 12:48 pm

  3. This doesn\’t work when you only add a user via the Site Settings > Site Collection Administrators.If the user is not added as a regular SharePoint user in any of the sites, this user will not return in the SPUserCollection on the rootweb object.

    Comment by Kurt — May 14, 2010 @ 8:11 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: