Wednesday 10 August 2011

List of all site administrators in a sharepoint 2007 farm

Hi All,

I have seen people trying to find out a way to enlist all site administrators of all subsites in their environment.
There is no out of box solution to achieve this. This is a very easy task to be done via object model of sharepoint.

I am sharing my code of a console application that will do the same. This just needs to be executed and no inputs  are required to be provided


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace YourNameSpace
{
    class Yourclassname
    {
        static void Main(string[] args)
        {
            SPFarm farm = SPFarm.Local;
            SPWebService webService = farm.Services.GetValue<SPWebService>("");
            foreach (SPWebApplication webApplication in webService.WebApplications)
            {
                foreach (SPSite site in webApplication.Sites)
                {
                    Console.WriteLine("The Site Owner for the Site Collection " + site.Url + " is" + site.Owner.LoginName +"\n");

                    using (site)
                    {
                        foreach (SPWeb web in site.AllWebs)
                        {
                            SPUserCollection webAdministrators = web.SiteAdministrators;
                            Console.WriteLine("Now processing the site named " + web.Title + " at the URL " + web.Url);
                            foreach (SPUser user in webAdministrators)
                            {
                                Console.WriteLine(user.LoginName + "\n");
                            }

                        }
                    }
                }
            }
            Console.ReadLine();


        }
    }
I hope this will help you out.
Thanks,
Rahul Rashu

No comments:

Post a Comment