- Site Collection Root
- Site 1: site title - "Products & Software"
- Subsite A: site title - "Widgets"
- Subsite AA: site title - "Widget X"
- Subsite BB: site title - "Widget Y"
- Site 2
- Site 3
protected override void OnLoad(EventArgs e) { // To ensure page behaves correctly, must call base.OnLoad(e). base.OnLoad(e); GetRootTitle(); } private void GetRootTitle() { string title = string.Empty; /* On admin pages, this label object doesn't exist and will throw a NullReferenceException if we don't check it before trying to use it. */ if (sectionTitle == null) return; using (SPSite site = new SPSite(SPContext.Current.Site.ID)) { using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID)) { title = IterateThroughParentsAndStoreInfo(web, title); } } sectionTitle.Text = title; // Set a label's value to the title. } private string IterateThroughParentsAndStoreInfo(SPWeb web, string title) { if (web.ParentWeb != null) { title = web.Title; return IterateThroughParentsAndStoreInfo(web.ParentWeb, title); } return title; }Note the using statements; these ensure there are no memory leaks by disposing of the objects properly and also implicitly handling the try/catch/finally block. The change to W0ut's code is the addition of the OnLoad() handler, which can be either in your master page or page layout. Also, we've added the title variable, which gets set in the recursive function; note that its value is always of the title for the site one before the last. This is because when the web variable's ParentWeb becomes null, we've reached the site collection. This is exactly what we need.
One last thing to note: We do a null check for the label object which will hold the title's value. On some administrative (system) pages, the label isn't rendered so we'll throw a NullReferenceException. Another way to do this is to always add the title label as a new Label(); this way it's always present, regardless of which page we're on.
No comments:
Post a Comment