Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

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

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

 
discard

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.