Enumerate all Site Collections in a SharePoint Farm

[Important News!]

binarywaste - profile picture - 1000x1000

I’ve kicked off a new project/band where I’m creating music with a heavier techno/metal sound called binarywaste.  You should check it out by clicking right here!

It’s funny, but I get asked this question allot, and thought a quick post on the subject explicitly calling it out would be helpful.

For various different reasons, you may have the need to enumerate over every site collection within your SharePoint Farm (Whether it be WSS 3.0 or MOSS 2007)

The easiest way to do this is to simply over each SPWebApplication found in the SPWebService.ContentService.WebApplications collection, and from there enumerate over each SPSite found in the SPWebApplication.Sites collection as follows:

// BEGIN EnumSiteCollections.cs
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace EnumSiteCollections
{
  class Program
  {
    static void Main(string[] args)
    {
      foreach (SPWebApplication wa in SPWebService.ContentService.WebApplications)
      {
        foreach (SPSite sc in wa.Sites)
        {
          try
          {
            Console.WriteLine("Do something with site at: {0}", sc.Url);
          }
          catch (Exception e)
          {
            Console.WriteLine("Exception occured: {0}\r\n{1}", e.Message, e.StackTrace);
          }
          finally
          {
            sc.Dispose();
          }
        }
      }
    }
  }
}
// END EnumSiteCollections.cs

The ContentService property of the SPWebService class allows you to get at the web applications running on the server.  Then from each one, it’s just a simple matter of enumerating the site collections contained in the Sites collection of the SPWebApplication.

This gives you a reference to all the site collections running on the farm (Less the Central Administration Web Application..Only “Content” web applications).

For console applications, ensure the user is a member of the local computers Administrators group.

Also, ensure that the running account has a full control policy for each web app you wish to enumerate.

See: How To: Create a Console Application from the WSS 3.0 SDK for more information.

HTH!

– Keith

12 Replies to “Enumerate all Site Collections in a SharePoint Farm”

  1. Hi,
    I was wondering if you ever used the Audit in sharepoint intensively.
    I tried to utilize it and it works fine other than a small “bug?”.
    I can’t seem to get the MachineName/IP in the audit entry. All the rest is fine.
    I searched for a way to contact you but that is the only one I managed to find.
    I really hope you can help me out with this.
    cheers,
    EJ

  2. Thank You.
    I didn’t even think about going to the web app level, but makes perfect sense and works great!

    1. Yes, you can.

      Use SiteData.GetContent() to get list of all SiteCollections within your content database.

      You need to pass the content database id and object type as contentdatabase

      Hope this helps!

Leave a Reply to Keith Richie Cancel reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s

%d bloggers like this: