Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I've got the following code that works well on most pages in SharePoint 2010; but it blows up on Site Actions > Site Settings > Manage site features with a NullReferenceException on the GetRootTitle() function. This is called from a custom master page (copy of v4.master):

protected string title = string.Empty;

protected override void OnLoad(EventArgs e)
{
      // To ensure page behaves correctly, must call base.OnLoad(e).
      base.OnLoad(e);

      GetRootTitle();
}
private void GetRootTitle()
{
   SPSite site = new SPSite(SPContext.Current.Site.ID);
   SPWeb web = site.OpenWeb(SPContext.Current.Web.ID);

   IterateThroughParentsAndStoreInfo(web);

   // Set a label's value to the title.
   sectionTitle.Text = title;

   web.Dispose();
   site.Dispose();
}

private void IterateThroughParentsAndStoreInfo(SPWeb web)
{
   // Do your code for the 'web' variable here.
   if (web.ParentWeb != null)
   {
      title = web.Title;
      using (SPWeb parentweb = web.ParentWeb)
      {
         IterateThroughParentsAndStoreInfo(parentweb);
      }
   }
}

Here's the error the http://myserver/mysite/_layouts/ManageFeatures.aspx page throws:

10/27/2011 13:59:46.92 w3wp.exe (0x26E8) 0x2334 SharePoint Foundation Runtime tkau Unexpected System.NullReferenceException: Object reference not set to an instance of an object. at ASP._8cc5acc7_8688_48f0_94fc_2a6d6124a691__164880475.GetRootTitle() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) aae55609-5b47-44c4-a883-752ad8cd2aa7

Update: For those wondering about the correct code, here it is (thanks to everyone's help!):

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;

  using (SPSite site = new SPSite(SPContext.Current.Site.ID))
  {
    using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
    {
      title = IterateThroughParentsAndStoreInfo(web, title);
    }
  }

  title = (title.Length >0) ? title : SPContext.Current.Site.RootWeb.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;
}

With the using clause, the CLR automatically wraps the code in try/catch/finally statements and will dispose of the objects correctly as well.

share|improve this question
    
Where are you defining the 'sectionTitle' control? –  Dave Wise Oct 27 '11 at 13:34
    
It's a label further down the page, where the old <SharePoint:ProjectProperty Property="Title" runat="server" /> was in the master page. –  Alex C Oct 27 '11 at 15:03

2 Answers 2

up vote 1 down vote accepted

Your Using clauses are in the wrong places. They should be around the 'site' and 'web' creations in GetRootTitle() and you should not have one around the parentWeb in IterateThroughParentsAndStoreInfo(). The way this is structured, it will dispose of the ParentWeb object in the second routine, effectively leaving original the 'web' object in an unknown state when it returns (i.e. disposing of a parent object usually disposes the children too)

share|improve this answer
    
Thanks, Dave. Is that what's causing the NullReferenceException? –  Alex C Oct 27 '11 at 14:32
1  
It can cause all manner of strange behavior but a null reference exception would be tops on the list of likely exceptions. –  Dave Wise Oct 27 '11 at 15:22

I think you got the answer to your question, but instead of all the iteration, you can just get to the root web title this way:

protected override void OnLoad(EventArgs e) {       
    base.OnLoad(e);        
    title  = SPContext.Current.Site.RootWeb.Title; 
} 
share|improve this answer
    
If all you need is the title of the RootWeb - do this. I'll have great sympathy for whoever inherits the code with all that iteration. –  James Love Oct 28 '11 at 8:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.