views:

34

answers:

2

Hi,

I need to set the page title (Page Title) of a SharePoint page in code. I have already tested

this.Page.Title = "My Page Title";

But this does not change the title when the page loads. Can anyone offer any advise on how to do this?

Thanks, MagicAndi

A: 

This blog post by Michael Becker gives a method of modifying the SharePoint Page title using the code below:

ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder) Page.Master.FindControl("PlaceHolderPageTitle");
contentPlaceHolder.Controls.Clear();
LiteralControl literalControl = new LiteralControl();
literalControl.Text = "My Page Title";
contentPlaceHolder.Controls.Add(literalControl); 
MagicAndi
+1  A: 

If you want to change the page title from a webpart on the page for example, you could use this:

private void ChangeTitle(string newTitle)
{
    SPListItem item = SPContext.Current.ListItem;

    if (item != null)
    {
        item[SPBuiltInFieldId.Title] = newTitle;
        item.SystemUpdate(false);
    }
}

This will only work for a page in the pages library, because the default.aspx page in the root of your site doesn't have an associated listitem. Also don't forget to refresh your page after changing the title.

The SystemUpdate makes sure that 'modified/modified by' information is not updated and that the version number doesn't increase. If you want this information updated, replace it by item.Update();

Tom Vervoort
Tom, useful info, +1.
MagicAndi
Tom, accepted as answer.
MagicAndi