Skip to content

Enumerate all Site Collections in a SharePoint Farm

September 5, 2008

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

Advertisement
11 Comments leave one →
  1. September 8, 2008 5:45 pm

    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. Xopher permalink
    May 12, 2009 12:38 am

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

  3. May 1, 2010 5:53 am

    Perfect! Thank you.

  4. Paal Amundsen permalink
    May 11, 2010 1:54 pm

    Is it possible to list all site collections using web services in stead of the object model?

    • May 11, 2010 5:19 pm

      I’m not aware of a web service that will do this for you, so you would have to roll your own.

    • Naga permalink
      January 11, 2011 1:58 am

      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!

  5. October 27, 2011 7:55 am

    Thank you !

    you saved my time !!!

Trackbacks

  1. Links (9/7/2008) « Steve Pietrek - Everything SharePoint
  2. WSS 3.0 & MOSS: Recopilatorio de enlaces interesantes (XXI)! - Blog del CIIN
  3. WSS 3.0 & MOSS: Recopilatorio de enlaces interesantes (XXI)! « Pasión por la tecnología…

Leave a Reply

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

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

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

Facebook photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.