Thursday, April 17, 2008

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

Alright 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.

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

SCAdmin.exe -url http://sharepoint.domain.com > 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,

http://sharepoint.domain.com/sites/SC1, domain\spadmin;
http://sharepoint.domain.com/sites/SC1, domain\spadmin; domain\user2; domain\user3
http://sharepoint.domain.com/sites/SC1, domain\user4; domain\user5;
http://sharepoint.domain.com/sites/SC1, domain\spadmin; domain\user6
http://sharepoint.domain.com/sites/SC1, domain\user7;
...............................
...................
........

6. 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();
sc.getSiteCollectionAdministrators(args);
}

private void getSiteCollectionAdministrators(string[] args)
{
try
{
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);
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://");
}
}
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;
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"))
{
SCCount++;
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)
{
Console.WriteLine(e.Message);
}
}

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


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

No comments: