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.
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>";
}
}
}
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. TheOpenWeb method is used to open the root Web site of the specified site collection.
string webUrl = "http://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();
}
}
Instead of using a foreach statement, the following example uses nested for statements to enumerate the list of subsite URLs and list titles.
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>";
}
}
}
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.
string webUrl = "http://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();
}
}
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.
SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
SPSiteCollection siteCollections = webApplication.Sites;
foreach (SPSite siteCollection in siteCollections)
{
Label1.Text += siteCollection.Url + "<BR>";
siteCollection.Close();
}
No comments:
Post a Comment
Thank you for Commenting Will reply soon ......