How to set SharePoint page title programmatically

I’ve spent some time today trying to figure out how to set the title of a SharePoint page from my own code. As blogger Michael Becker rightly points out, you can’t simply set Page.Title.

The correct solution, as provided by Michael, is illustrated in this example C# code:

// Get a reference to the appropriate Content Placeholder
ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder) 
                       Page.Master.FindControl("PlaceHolderPageTitle");

// Clear out anything that SharePoint might have put in it already
contentPlaceHolder.Controls.Clear();

// Put your content in
LiteralControl literalControl = new LiteralControl();
literalControl.Text = "Your text goes here";
contentPlaceHolder.Controls.Add(literalControl);

Happily this even works when you “cheat” by hosting an ASP.NET user control within a SmartPart, as opposed to creating a bona fide Web Part.

If you enjoyed this post, please share it!

    7 comments on “How to set SharePoint page title programmatically

    1. Lars says:

      Thank you!!!!

    2. Bilawal Asghar says:

      This does not work for me,in sharepoint sandbox solution on
      ontentPlaceHolder contentPlaceHolder = (ContentPlaceHolder)
      Page.Master.FindControl(“PlaceHolderPageTitle)
      gives me error null exception, why??

    3. Florin says:

      Thanks,
      This is a great post.
      I got such a headache looking for this solution until I found your article.

    4. Ignacio says:

      Thank you! This works!

    5. Carlos CErrato says:

      Thanks! Really useful post!!!

    6. Michael G. says:

      Thank’s for that,
      in SP 2013 this worked for me on a none publishing site with placeholder “PlaceHolderPageTitleInTitleArea”.

    7. Robi says:

      This code will fail in SharePoint 2013 publishing sites if someone set the page title in “Edit SEO Properties”.
      In this case this code should be used:

      ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder)page.Master.FindControl("PlaceHolderPageTitle");
      contentPlaceHolder.SetRenderMethodDelegate(delegate(System.Web.UI.HtmlTextWriter writer,
      System.Web.UI.Control Container)
      {
      writer.Write(title);
      });

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>