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