Programmatically Set the Title of a SharePoint Page?
Asked Answered
A

2

7

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

Apyretic answered 6/8, 2010 at 11:11 Comment(0)
T
5

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

Trochal answered 10/8, 2010 at 10:17 Comment(0)
A
8

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); 
Apyretic answered 6/8, 2010 at 11:16 Comment(0)
T
5

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

Trochal answered 10/8, 2010 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.