Saturday, September 19, 2009

Get all the sub sites under the current site

Scenario:
You want to get all the sub sites under a given site collection including sub sites under sub sites.
Scenario:
Use site.AllWebs to get the entire collection as shown below.
Code:

void GetAllSitesUnderSiteCollection(string siteUrl)
{
using (SPSite site = new SPSite(siteUrl))
{
foreach (SPWeb web in site.AllWebs)
{
Console.WriteLine(web.Title + " ~ " + web.Url);
}
}
}

No comments: