如何:枚举网站和网站集

上次修改时间: 2010年4月12日

适用范围: SharePoint Foundation 2010

在 SharePoint Online 中提供

您可以返回网站集中的所有网站、网站下的所有首层子网站、当前网站的所有子网站和列表或者 SharePoint Web 应用程序中网站集的集合,如下面的示例所示。

其中每个示例都要求对 Microsoft.SharePointMicrosoft.SharePoint.Utilities 命令空间使用 using 指令(在 Visual Basic 中为 Imports)。最后一个示例另外还要求导入 Microsoft.SharePoint.Administration 命名空间。

可以在应用程序页的代码隐藏文件中的 Button_Click 处理程序中运行以下代码段,此页必须包含一个要用来显示结果的标签。有关如何创建在 SharePoint Foundation 上下文中使用的应用程序页的信息,请参阅为 SharePoint 创建应用程序页(该链接可能指向英文页面)。每个示例都假定 .aspx 页上存在一个 Label 控件。

SPSite 类的 AllWebs 属性返回网站集中的所有网站(包括首要网站和所有子网站)。下面的示例枚举当前网站集中所有网站和列表的标题。

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>";
        }
    }
}

若要返回某个网站下的所有首层子网站的列表,请使用 SPWeb 类的 Webs 属性。下面的示例显示子网站标题的列表。OpenWeb() 方法用于打开指定网站集的根网站。

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();
    }
}

下面的示例不使用 foreach 语句,而是使用嵌套 for 语句来枚举子网站 URL 的列表及列表标题。

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>";
        }
    }
}

下一个代码示例显示当前网站的所有子网站和列表以及每个列表中项的数目。此示例使用嵌套 foreach 语句来循环访问网站和列表集。

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();
    }
}

若要返回 SharePoint Web 应用程序中网站集的集合,请使用 Microsoft.SharePoint.Administration.SPWebApplication 类的 Sites 属性。使用 Microsoft.SharePoint.SPContext 类的属性可返回当前 Web 应用程序。下面的代码示例显示当前 Web 应用程序中所有网站集的 URL。

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();
}

为了提高性能,本主题中的示例释放了枚举集合时实例化的网站集和网站对象。有关释放 SharePoint Foundation 对象的最佳做法的信息,请参阅释放对象

请参阅

引用

Microsoft.SharePoint

概念

使用列表对象和集合

使用 Visual Studio 进行 SharePoint 开发

安全验证并进行发布以更新数据

特权提升