Tuesday, May 7, 2013

Get all first level subsites under website in SharePoint 2010

In this post we will discuss how to retrieve all first level sub sites under a site or site collection. Also you can check out my previous posts on Powershell command to install activate feature SharePoint 2010, SharePoint 2010 Interview Questions and Answers and Reindex feature in list and document library in SharePoint 2013.

We have to use the Webs property of SPWeb class to get the first level subsites of a specific website. Below is the full SharePoint 2010 object model code:
string webUrl = "http://Site Collection URL";

using (SPWeb oWebsite = new SPSite(webUrl).OpenWeb())
    SPWebCollection collWebsite = oWebsite.Webs;

    foreach (SPWeb subSite in collWebsite)
        Label1.Text += subSite.Title + " ";


The above code will show all the web sites under the specific site line by line.

No comments:

Post a Comment