SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given a path, e.g. http://sp2010/departments/, how can you get a list of site collections that are underneath it (which have been created through the central administration site)?

Basically want to build up a list of links on the 'departments' site, linking to all the sites underneath it (url wise, rather than as sub-sites created through the site), if possible, showing items from a list (named something like announcements, or news).

All the site collections involved are in the same same web application, so it should work getting the list via code (C#), which can then be put on the page as a web part (or editing the Master Page)?

Edit Expanding on this question a bit, since it is somewhat related, but can you do the same for listing site collections via PowerShell? This is to enable backups of all sites under that path as well.

share|improve this question

Iterate over the SPWebApplication.Sites property, checking the url of each site as you go. Maybe something like

var sites = webapp.Sites.Where(i=>i.url.StartsWith(webapp.url + "/departments/"));

If you want to get list items from a site collection use a SPDataQuery or CrossListQueryCache rather than iterating, as its much much faster.

Both would be rendered via a webpart (no way to do this via the masterpage).

share|improve this answer
Using Powershell you can use the Get-SPWebApplication | Get-SPSite snippet to iterate over the site collecitons – Dennis G Nov 18 '10 at 10:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.