This documentation is archived and is not being maintained.

How to: Return Sites and Site Collections

Windows SharePoint Services 3

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.

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.

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.

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.

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.

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.

Show: