Changing the Window Title programmatically in SP2010

Changing the Window Title programmatically in SP2010

SharePoint uses a placeholder with the name “PlaceHolderPageTitle” to influence the window title. You can set the window title by putting text in this placeholder. But if you want the window title to be based on other parameters, let’s say the current user, it is also possible to change the window title programmatically.

In the code snippet below I use a webcontrol to add the current username to the window title. Furthermore, the text “[Administrator]” is added to the window title if the current use is a site collection administrator.

public class SPPageTitleChanger: WebControl
    {
        protected override void OnLoad(EventArgs e)
        {
            SPUser currentUser = SPContext.Current.Web.CurrentUser;
            this.ChangePageTitle(currentUser.Name, currentUser.IsSiteAdmin ? "[Administrator]" : String.Empty);
        }

        private void ChangePageTitle(String titlePrefix, String titleSuffix)
        {
            ContentPlaceHolder TitlePlaceHolder = FindChildControl<ContentPlaceHolder>(this.Page, "PlaceHolderPageTitle");
            if (TitlePlaceHolder != null)
            {
               if (!String.IsNullOrEmpty(titlePrefix))
                {
                    LiteralControl newTitle = new LiteralControl(String.Format("{0} - ", titlePrefix));
                    TitlePlaceHolder.Controls.AddAt(0, newTitle);
                }
                if (!String.IsNullOrEmpty(titleSuffix))
                {
                    LiteralControl newTitle = new LiteralControl(String.Format(" - {0}", titleSuffix));
                    TitlePlaceHolder.Controls.Add(newTitle);
                }
            }
        }

        private T FindChildControl<T>(Control startingControl, string id) where T : Control
        {
            T found = null;
            foreach (Control activeControl in startingControl.Controls)
            {
                found = activeControl as T;
                if (found == null || (string.Compare(id, found.ID, true) != 0))
                {
                    found = FindChildControl<T>(activeControl, id);
                }
                if (found != null)
                {
                    break;
                }
            }
            return found;
        }
     }
}

The result of this code will be something like the image below. Note the “QUAHOG\Administrator” and the “[Administrator]” text in the window title.

Changed window title in SP2010

You can wrap this code in a webcontrol as I did:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Control Id="AdditionalPageHead"
         Sequence="10"
         ControlAssembly="AlainDeKlerk.SP2010.SharePointTitle, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9967ca7f84f28fdd"
         ControlClass="AlainDeKlerk.SP2010.SharePointTitle.SPPageTitleChanger">
  </Control>
</Elements>

Don’t forget to add a SafeControl entry in your web.config or the code won’t run!

About the author

Alain

You can leave a response, or trackback from your own site.

Leave a Reply