Nothing new or exciting here, but I had to do this recently, and I couldn’t find out how to do it quickly…
I needed to set the text in the browser’s title bar based on some data that is being pulled from a database and displayed in a web part. This would be completely trivial in a plain old ASP.NET page. It was a little more complicated in SharePoint.
SharePoint wants to control the title itself so that it can be set based on the value you supply when you edit the properties for the page in the Document Library where it lives. For this reason, there’s a Content Placeholder dropped into the title tag in the Master Page. You need to update the value of this Content Placeholder in order to update the title tag. You can’t just set Page.Title.
So the sequence goes:
- Get a reference to the appropriate Content Placeholder
- Clear out anything that SharePoint might have put in it already
- Put your content in.
Or, in C#:
ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder) Page.Master.FindControl("PlaceHolderPageTitle");
LiteralControl literalControl = new LiteralControl();
literalControl.Text = "Your text goes here";
So, obviously not rocket science… But it took me longer than it should have none the less…