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.

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

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 = "http://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

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

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 = "http://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

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

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: