Link to home
Start Free TrialLog in
Avatar of ThatSharepointGuy
ThatSharepointGuyFlag for Japan

asked on

Return all Site Collection Administrators

Aside from some other problems that I've been having permissions wise...I wanted to see if someone else might be able to chip in on this:

I've created a web part that should display all of the site collection administrators, for each site collection within the current web application.

However, it doesn't return all of them?!

The output looks something like this:

------------------------------------------------------
SPContext.Current.Site.WebApplication.Name = MOSS_Dev

Site Name: SPSite Url=http://SERVER/SC1
    SCAdmin = DOM\sharepoint.admin

Site Name: SPSite Url=http://SERVER/SC2
    SCAdmin = DOM\sharepoint.dev
    SCAdmin = DOM\sharepoint.admin

Site Name: SPSite Url=http://SERVER/SC3
    SCAdmin = DOM\sharepoint.user

Site Name: SPSite Url=http://SERVER/SC4
    SCAdmin = DOM\sharepoint.admin

Site Name: SPSite Url=http://SERVER/SC5
    SCAdmin = DOM\sharepoint.dev
    SCAdmin = DOM\sharepoint.admin

Site Name: SPSite Url=http://SERVER/SC6
    SCAdmin = DOM\sharepoint.admin

Site Name: SPSite Url=http://SERVER/SC7

Site Name: SPSite Url=http://SERVER/SC8
    SCAdmin = DOM\sharepoint.dev
    SCAdmin = DOM\sharepoint.admin

Searched 8 Site Collections.
------------------------------------------------------

Now, "DOM\sharepoint.admin", "DOM\sharepoint.dev", and "DOM\sharepoint.user" are ALL THREE listed as Site Collection Administrators.

the "DOM\sharepoint.admin" account is listed on each site collection as an administrator when it is created, thus it shows up in Central Administration > Application Management > Site Collection Administrators list.  However, that only allows 2 SC admins.  

So, to test (because in our environment we have 8 total SC admins for all the site collections), I added "DOM\sharepoint.dev" and "DOM\sharepoint.user" to the Site Actions > Site Settings > Site Collection Administrators group on the site collections themselves.

However, the code that I've posted doesn't show any sort of "pattern" in what it displays.
All 8 Site Collections have the same settings....
"DOM\sharepoint.admin" listed as a SC Admin in Central Administration.
"DOM\sharepoint.dev" "DOM\sharepoint.user" are listed on the site collection itself in the Site Collection Administrators group.

Yet when I run this code in a web part...
"DOM\sharepoint.admin" is missing from /SC3 and /SC7.
"DOM\sharepoint.dev" is missing from /SC1, /SC3, /SC4, /SC6, /SC7.
"DOM\sharepoint.user" is missing from /SC1, /SC2, /SC4, /SC5, /SC6, /SC7, and /SC8.

I don't get why it's not returning the rest, and why it's so random?  They all have SAME EXACT SETTINGS.  It's so strange, and it's driving me crazy!

I can find no other bit of code in the SDK or MSDN other than "oUser.IsSiteAdmin" to determine if someone is in the Site Collection Administrators group.

Can anyone help?
using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;


namespace My.SharePoint.WebParts
{
    public class SCAdmins : Microsoft.SharePoint.WebPartPages.WebPart
    {
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            // Get references to the web application
            SPWebApplication webApp = SPContext.Current.Site.WebApplication;
           
            // test...
            writer.Write("SPContext.Current.Site.WebApplication.Name = <b>" + webApp.Name + "</b><br/>");
            
            // Set up vars
            SPSite oSite = null;
            SPWeb oWeb = null;
            int SCCount = 0;

            // Retreive all Site Collections within webApp
            foreach (SPSite siteCollection in webApp.Sites)
            {
                // counter, just to see how many site collections it went through.
                SCCount++;
                
                // output the current site collection's name
                writer.Write("Site Name: <b>" + siteCollection + "</b><br/>");
                oSite = new SPSite(siteCollection.Url);
                oWeb = oSite.OpenWeb();

                // get collection of users in the site
                SPUserCollection userCollection = oWeb.AllUsers;
                
                // iterate through all users
                foreach (SPUser oUser in userCollection)
                {
                    // if the user is a site collection administrator
                    if (oUser.IsSiteAdmin)
                    {
                        // display his/her login name
                        writer.Write("&nbsp;&nbsp;&nbsp; SCAdmin = <b>" + oUser.LoginName + "</b><br/>");
                    }
                }
            }
            writer.Write("<br/><br/>Searched " + SCCount + " Site Collections.");
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Hairbrush
Hairbrush
Flag of Jersey image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ThatSharepointGuy

ASKER

It doesn't exactly return all Site Collection Administrators.  The "SPSite.Owner" just shows who is listed as the site owner.  It won't show all users that are listed in CA and/or SC level Admins.

Thanks though!