Partager via


How to: Return Sites and Site Collections

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

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.

You can return all the Web sites within a site collection, including the top-level site and all subsites, by using the AllWebs property of the SPSite class. The following example displays the titles of all the sites and lists in the current site collection.

Dim mySite As SPSite = SPContext.Current.Site
Dim subSites As SPWebCollection = mySite.AllWebs
Dim i As Integer

For i = 0 To subSites.Count - 1
    Dim lists As SPListCollection = subSites(i).Lists
    Dim j As Integer

    For j = 0 To lists.Count - 1
        Response.Write(SPEncode.HtmlEncode(subSites(i).Title) 
            & " :: " _ & SPEncode.HtmlEncode(lists(j).Title) & "<BR>")
    Next j
Next i
SPSite mySite = SPContext.Current.Site;
SPWebCollection subSites = mySite.AllWebs;

for (int i=0;i<subSites.Count;i++)
{
    SPListCollection lists = subSites[i].Lists;

    for (int j=0;j<lists.Count;j++)
    {
        Response.Write(SPEncode.HtmlEncode(subSites[i].Title) 
            + " :: " + SPEncode.HtmlEncode(lists[j].Title) + "<BR>");
    }
}

The previous example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.

To return a list of all the first-tier subsites beneath a Web site, use the Webs property of the SPWeb class. The following example displays a list of subsite titles.

Dim mySite As SPWeb = SPContext.Current.Web
Dim sites As SPWebCollection = mySite.Webs
Dim subSite As SPWeb

For Each subSite In  sites
    Response.Write(SPEncode.HtmlEncode(subSite.Title) & "<BR>")
Next subSite
SPWeb mySite = SPContext.Current.Web;
SPWebCollection sites = mySite.Webs;

foreach (SPWeb subSite in sites)
{
    Response.Write(SPEncode.HtmlEncode(subSite.Title) + "<BR>");
}

The previous example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.

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

Dim mySite As SPWeb = SPContext.Current.Web
Dim sites As SPWebCollection = mySite.Webs
Dim i As Integer

For i = 0 To sites.Count - 1
    Dim lists As SPListCollection = sites(i).Lists
    Dim j As Integer

    For j = 0 To lists.Count - 1
        Response.Write(sites(i).Url & " :: " 
            & SPEncode.HtmlEncode(lists(j).Title) & "<BR>")
    Next j
Next i
SPWeb mySite = SPContext.Current.Web;
SPWebCollection sites = mySite.Webs;

for (int i=0;i<sites.Count;i++)
{
    SPListCollection lists = sites[i].Lists;

    for (int j=0;j<lists.Count;j++)
    {
        Response.Write(sites[i].Url + " :: " 
            + SPEncode.HtmlEncode(lists[j].Title) + "<BR>");
    }
}

The previous example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.

The next 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 mySite As SPWeb = SPContext.Current.Web
Dim sites As SPWebCollection = mySite.Webs
Dim subSite As SPWeb

For Each subSite In  sites
    Response.Write("<B>" & SPEncode.HtmlEncode(subSite.Title) & "</B>" 
        & "<BR>")

    Dim lists As SPListCollection = subSite.Lists
    Dim list As SPList

    For Each list In  lists
        Response.Write(SPEncode.HtmlEncode(list.Title) & " :: " 
           & list.ItemCount & "<BR>")
    Next list
Next subSite 
SPWeb mySite = SPContext.Current.Web;
SPWebCollection sites = mySite.Webs;

foreach (SPWeb subSite in sites)
{
    Response.Write("<B>" + SPEncode.HtmlEncode(subSite.Title) + "</B>" 
        + "<BR>");

    SPListCollection lists = subSite.Lists;

    foreach (SPList list in lists)
    {
        Response.Write(SPEncode.HtmlEncode(list.Title) 
            + " :: " + list.ItemCount + "<BR>");
    }
}

The previous example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.

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 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
Dim siteCollection As SPSite

For Each siteCollection In siteCollections
   Response.Write(siteCollection.Url + "<BR>")
Next siteCollection
SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
SPSiteCollection siteCollections = webApplication.Sites;

foreach (SPSite siteCollection in siteCollections)
{
   Response.Write(siteCollection.Url + "<BR>");
}

The previous example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint, Microsoft.SharePoint.Administration, and Microsoft.SharePoint.Utilities namespaces.

The example assumes the existence of an .aspx page that contains a label control.

See Also

Reference

Microsoft.SharePoint

Concepts

Working with List Objects and Collections

Getting Started with Programmatically Customizing a SharePoint Web Site in Visual Studio

Security Validation and Making Posts to Update Data

Elevation of Privilege