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.

1 Answer 1


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).

  • Using Powershell you can use the Get-SPWebApplication | Get-SPSite snippet to iterate over the site collecitons
    – Dennis G
    Commented Nov 18, 2010 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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