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
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://&quot;);
                        }
                    }
                    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!

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://&quot;, 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.

April 15, 2008

April 2008

Filed under: SharePoint 2007 — brij28 @ 7:43 pm
How to calculate age of list items in SharePoint:

Here are the steps to calculate age of the list items in SharePoint,
 
1. Go to the list where you want to calculate age of the items e.g. Task List
2. Click Settings => List Settings and create a new column and name it e.g. "Today", There is no need to select any specific type for this column (Just type the name of the column and click "OK" at the bottom)
3. Create one more column and name it e.g. "Age in Days". Select "Calculated(calculation based on other columns)" type and provide formula using "Today" column which you created in step 2 e.g. I have entered
=[Today]-[Created]. Select "number" for the data type and 0(zero) in "Number of Decimal places". So basically it will calculate difference between today’s date and date the item was created.
4. After 3rd step, remove "Today" column, which you have created in step 2. When you remove Today column, SharePoint automatically refers "Today" (which has been deleted now) as the today’s date in the formula calculation.
5. Go back to Task List and it should display the number of days under the "Age in Days" column of each items in Task List.

Create a free website or blog at WordPress.com.