How to: Enumerate Sites and Site Collections

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.

Dim oSiteCollection As SPSite = SPContext.Current.Site
Dim collWebsite As SPWebCollection = oSiteCollection.AllWebs

For i As Integer = 0 To collWebsite.Count - 1
    Using oWebsite As SPWeb = collWebsite(i)
        Dim collList As SPListCollection = oWebsite.Lists
        
        For j As Integer = 0 To collList.Count - 1
            Label1.Text += SPEncode.HtmlEncode(collWebsite(i).Title) & " " & SPEncode.HtmlEncode(collList(j).Title) & "<BR>"
        Next
    End Using
Next
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.

Dim webUrl As String = "https://Server/sites/SiteCollection"
Using oWebsite As SPWeb = New SPSite(webUrl).OpenWeb()
    Dim collWebsite As SPWebCollection = oWebsite.Webs
    
    For Each subSite As SPWeb In collWebsite
        Label1.Text += SPEncode.HtmlEncode(subSite.Title) & "<BR>"
        
        subSite.Close()
    Next
End Using
string webUrl = "https://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.

Dim oWebsite As SPWeb = SPContext.Current.Web
Dim subSites As SPWebCollection = oWebsite.Webs

For i As Integer = 0 To subSites.Count - 1
    Using subSite As SPWeb = subSites(i)
        Dim collList As SPListCollection = subSite.Lists
        
        For j As Integer = 0 To collList.Count - 1
            Label1.Text += (subSite.Url & " :: ") + SPEncode.HtmlEncode(collList(j).Title) & "<BR>"
        Next
    End Using
Next
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.

Dim webUrl As String = "https://Server/sites/SiteCollection"
Using oWebsite As SPWeb = New SPSite(webUrl).OpenWeb()
    Dim subSites As SPWebCollection = oWebsite.Webs
    
    For Each subSite As SPWeb In subSites
        Label1.Text += SPEncode.HtmlEncode(subSite.Title) & "<BR>"
        
        Dim collList As SPListCollection = subSite.Lists
        
        For Each oList As SPList In collList
            Label1.Text += SPEncode.HtmlEncode(oList.Title) & "   " &
                oList.ItemCount.ToString() & "<BR>"
        Next
        
        subSite.Close()
    Next
End Using
string webUrl = "https://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.

Dim webApplication As SPWebApplication = SPContext.Current.Site.WebApplication
Dim siteCollections As SPSiteCollection = webApplication.Sites

For Each siteCollection As SPSite In siteCollections
    Label1.Text += siteCollection.Url & "<BR>"
    
    siteCollection.Close()
Next
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.

See Also

Reference

Microsoft.SharePoint

Concepts

Working with List Objects and Collections

Using Visual Studio for SharePoint Development

Security Validation and Making Posts to Update Data

Elevation of Privilege