Iterate all webs in site collection

Below is the code to iterate all webs under a site collection.

private static void ItetrateAllWebsInSiteCollection()
{
using (SPSite oSPsite = new SPSite("http://sitecollectionurl"))
{
using (SPWeb oSPWeb = oSPsite.OpenWeb())
{
Console.WriteLine(oSPWeb.Url);
if (oSPWeb.Webs.Count > 0)
{
CheckIfSubSiteExsist(oSPWeb);
}
}
}
}
private static void CheckIfSubSiteExsist(SPWeb oSPWeb)
{
foreach (SPWeb web in oSPWeb.Webs)
{
if (web.Webs.Count > 0)
{
CheckIfSubSiteExsist(web);
}
Console.WriteLine(web.Url);
web.Dispose();
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s