利用 SPSite类的 AllWebs属性,您可以返回一个网站集下的所有网站,包括顶级网站和所有子网站。下面的例子显示了在当前的网站集下的所有网站和列表的标题。
VB
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
C#
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> " );
}
}
上面的例子需要引用Microsoft.SharePoint和Microsoft.SharePoint.Utilities命名空间。
如果我们要返回某网站下的所有第一层子站点的清单,可以使用SPWeb类的Webs属性。下面是示例代码:
VB
Dim sites As SPWebCollection = mySite.Webs
Dim subSite As SPWeb
For Each subSite In sites
Response.Write(SPEncode.HtmlEncode(subSite.Title) & " <BR> " )
Next subSite
C#
SPWebCollection sites = mySite.Webs;
foreach (SPWeb subSite in sites)
{
Response.Write(SPEncode.HtmlEncode(subSite.Title) + " <BR> " );
}
上面的例子同样需要引用Microsoft.SharePoint和Microsoft.SharePoint.Utilities这两个命名空间。
下面的例子用for语句代替了foreach,处理相同的工作:
VB
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
C#
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> " );
}
}
这端代码同样需要引用Microsoft.SharePoint和Microsoft.SharePoint.Utilities这两个命名空间。
接下来的例子将显示当前网站下的所有子网站和列表,同时显示各列表中的条目数量。通过嵌套foreach语句来遍历所有的网站和列表集合。
VB
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
C#
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> " );
}
}
同样,本例也需要引用那2个命名空间。
如果要返回某SharePoint Web应用程序下所有的网站集的集合,可以使用Microsoft.SharePoint.Administration.SPWebApplication类的Sites属性。通过Microsoft.SharePoint.SPContent类的属性可以返回当前的Web应用程序。下面的样例显示了当前Web应用下所有网站集的URL地址。
Dim siteCollections As SPSiteCollection = webApplication.Sites
Dim siteCollection As SPSite
For Each siteCollection In siteCollections
Response.Write(siteCollection.Url + " <BR> " )
Next siteCollection
C#
SPSiteCollection siteCollections = webApplication.Sites;
foreach (SPSite siteCollection in siteCollections)
{
Response.Write(siteCollection.Url + " <BR> " );
}
上面的例子需要引用Microsoft.SharePoint,Microsoft.SharePoint.Administration,以及 Microsoft.SharePoint.Utilities命名空间,并假设位于一个包含label控件的.aspx页面中。