Iterate through Sites in a specific Level in SharePoint 2010
How do I iterate through all my sites from a specific level. At the moment, I have sites in the following location: http://myintranet.com/sites.
What I want to do is output all my sites from /sites, since I have other child sites beneath that level.
How can I do this?
At the moment, my code looks like this:
private void GetNavigation()
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPWeb intranetSite = new SPSite(SPContext.Current.Web.Url + "/sites/").OpenWeb())
{
SPWebCollection subSites = intranetSite.Webs;
NavigationOutputHtml.Text = CreateNavigationHtml(开发者_运维技巧subSites);
}
});
}
I am doing all my iteration in the "CreateNavigationHtml" method. But I can't seem to pass the correct sites into my method from SPWebCollection.
Thanks for any help.
/sites
is not a web site, it's a Managed Path. In SharePoint, an IIS web site is a Web Application. Managed Paths allow SharePoint to have multiple Site Collections within a single IIS web site. And then SharePoint Sites are bundled within a Site Collection.
So to get all Sites within all Site Collections within the Web Application's /sites
Managed Path, you will need code like this:
foreach (SPSite site in SPContext.Current.Site.WebApplication.Sites)
{
try
{
if (SPSite.Exists(new Uri(site.Url)) && site.ServerRelativeUrl.StartsWith("/sites/"))
{
SPWebCollection subSites = site.AllWebs;
NavigationOutputHtml.Text = CreateNavigationHtml(subSites);
}
}
finally
{
site.Dispose();
}
}
精彩评论