Feb
19
2010

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:
  • Twitter
  • Facebook
  • del.icio.us
  • Reddit
  • Digg
  • StumbleUpon
  • Technorati
  • email
  • RSS

Written by Chris Barnes in: ASP.NET | Tags: , , , , ,

1 Comment »

RSS feed for comments on this post. TrackBack URL


Leave a Reply

Proudly powered by WordPress and W2-S Internet Services. Design based on Aeros Theme by TheBuckmaker.com.