[Important News!]
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
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
Thank You.
I didn’t even think about going to the web app level, but makes perfect sense and works great!
Perfect! Thank you.
Is it possible to list all site collections using web services in stead of the object model?
I’m not aware of a web service that will do this for you, so you would have to roll your own.
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!
Cool, thanks for sharing Naga.
Thank you !
you saved my time !!!
Reblogged this on Points of SharePoint.