Export (0) Print
Expand All

How to: Enumerate Sites and Site Collections

Last modified: April 12, 2010

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

You can return all the Web sites within a site collection, all the first-tier subsites beneath a Web site, all the subsites and lists for the current Web site, or the collection of site collections in a SharePoint Web application, as shown in the following examples.

Each of the examples require using directives (Imports in Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces. The last example additionally requires importing the Microsoft.SharePoint.Administration namespace.

You can run the following snippets in a Button_Click handler from the code behind for an application page, which must contain a label in which to display results. For information about how to create an application page that works in the context of SharePoint Foundation, see Creating Application Pages for SharePoint. Each example assumes the existence of a Label control on the .aspx page.

The AllWebs property of the SPSite class returns all the Web sites within a site collection, including the top-level site and all subsites. The following example enumerates the titles of all the Web sites and lists in the current site collection.

SPSite oSiteCollection = SPContext.Current.Site;
SPWebCollection collWebsite = oSiteCollection.AllWebs;

for (int i = 0; i < collWebsite.Count; i++)
{
    using (SPWeb oWebsite = collWebsite[i])
    {
        SPListCollection collList = oWebsite.Lists;

        for (int j = 0; j < collList.Count; j++)
        {
            Label1.Text += SPEncode.HtmlEncode(collWebsite[i].Title) + "   "
                + SPEncode.HtmlEncode(collList[j].Title) + "<BR>";
        }
    }
}

To return a list of all the first-tier subsites beneath a specific Web site, use the Webs property of the SPWeb class. The following example displays a list of subsite titles. The OpenWeb() method is used to open the root Web site of the specified site collection.

string webUrl = "http://Server/sites/SiteCollection";

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

    foreach (SPWeb subSite in collWebsite)
    {
        Label1.Text += SPEncode.HtmlEncode(subSite.Title) + "<BR>";
        subSite.Close();
    }
}

Instead of using a foreach statement, the following example uses nested for statements to enumerate the list of subsite URLs and list titles.

SPWeb oWebsite = SPContext.Current.Web;
SPWebCollection subSites = oWebsite.Webs;

for (int i = 0; i < subSites.Count; i++)
{
    using (SPWeb subSite = subSites[i])
    {
        SPListCollection collList = subSite.Lists;

        for (int j = 0; j < collList.Count; j++)
        {
            Label1.Text += subSite.Url + "   " + 
                SPEncode.HtmlEncode(collList[j].Title) + "<BR>";
        }
    }
}

The next code example displays all the subsites and lists for the current Web site, as well as the number of items in each list. The example uses nested foreach statements to iterate through the collections of Web sites and lists.

string webUrl = "http://Server/sites/SiteCollection";
using (SPWeb oWebsite = new SPSite(webUrl).OpenWeb())
{
    SPWebCollection subSites = oWebsite.Webs;

    foreach (SPWeb subSite in subSites)
    {
        Label1.Text += SPEncode.HtmlEncode(subSite.Title) + "<BR>";

        SPListCollection collList = subSite.Lists;

        foreach (SPList oList in collList)
        {
            Label1.Text += SPEncode.HtmlEncode(oList.Title) + "   " +
                oList.ItemCount.ToString() + "<BR>";
        }

        subSite.Close();
    }
}

To return the collection of site collections in a SharePoint Web application, use the Sites property of the Microsoft.SharePoint.Administration.SPWebApplication class. Use properties of the Microsoft.SharePoint.SPContext class to return the current Web application. The following code example displays the URLs of all the site collections in the current Web application.

SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
SPSiteCollection siteCollections = webApplication.Sites;

foreach (SPSite siteCollection in siteCollections)
{
    Label1.Text += siteCollection.Url + "<BR>";

    siteCollection.Close();
}

To improve performance, the examples in this topic dispose of site collection and Web site objects that it instantiates when enumerating collections. For information about best practices for disposing SharePoint Foundation objects, see Disposing Objects.

Show:
© 2014 Microsoft