Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Set the Title of a SharePoint Page?

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

like image 555
Tangiest Avatar asked Aug 06 '10 11:08

Tangiest


2 Answers

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); 
like image 154
Tangiest Avatar answered Sep 30 '22 14:09

Tangiest


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();

like image 36
Tom Vervoort Avatar answered Sep 30 '22 15:09

Tom Vervoort