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
[...] Enumerate all Site Collections in a SharePoint Farm [...]
Pingback by Links (9/7/2008) « Steve Pietrek - Everything SharePoint — September 8, 2008 @ 5:34 am
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
Comment by EJ — September 8, 2008 @ 5:45 pm
[...] enumerar todos los site collections de una granja de SharePoint. Fuente: Blog de [...]
Pingback by WSS 3.0 & MOSS: Recopilatorio de enlaces interesantes (XXI)! - Blog del CIIN — September 24, 2008 @ 3:44 am
[...] enumerar todos los site collections de una granja de SharePoint. Fuente: Blog de [...]
Pingback by WSS 3.0 & MOSS: Recopilatorio de enlaces interesantes (XXI)! « Pasión por la tecnología… — September 24, 2008 @ 3:49 am