Sitefinity Code to Configure Page Title

What we want to achieve?
– To modify the Page Title to “ABC Inc | <Page_Title>” on the fly

How to achieve in Webform Template?
1/ Make sure the HTML Template has included the <title> tag
2/ Add an HTML id to the <title> tag
3/ Webform Template:
Add the title tag with id attribute:

<html>
    <head runat="server">
        <title id="page_title"></title>
    </head>
    <body runat="server">
        <p>Lorem Ipsum</p>
    </body>
</html>

Update page title at Page_Load override method:

protected void Page_Load(object sender, EventArgs e)
{
    var man = PageManager.GetManager();
 
    if (SiteMapBase.GetActualCurrentNode() != null)
    {
        try
        {
            var getCurrentPage = SiteMapBase.GetActualCurrentNode().Id;
            if (getCurrentPage != Guid.Empty)
            {
                page_title.Text = page.Title != null ? string.Format("ABC Inc | {0}", page.Title.ToString()) : "ABC Inc | Apple Balloon Crazy";
            }
        }
        catch (Exception ex)
        {
            Log.Write("Template ERROR. " + ex.Message + " " + ex.StackTrace);
            page_title.Text = "ABC Inc | Apple Balloon Crazy";
        }
    }
}

4/ MVC Widget Virtual Page:
We have some pages are “virtual” page, not the physical page setup in Sitefinity.
Therefore, we need to update the code itself at .cshtml

@{
    var page = (Page)HttpContext.Current.Handler;
    ((System.Web.UI.HtmlControls.HtmlTitle)page.Header.FindControl("page_title")).Text = <your_page_title> != null ? string.Format("ABC Inc | {0}", <your_page_title>) : string.Format("{0}", page.Title);
}

References:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s