Inconvenient Page Title <title> element
During my last Web Content Management (WCM) project built on top of Microsoft Office SharePoint Server (MOSS) 2007 I noticed that SharePoint was rendering two <title> elements on all pages:
The first title element was the one defined by me in the Master Page. That title element correctly displayed the title of the page using a custom control:
For some reason SharePoint was automatically adding the second – empty title element during the rendering process of the page.
Having two title elements in an (X)HTML page is not allowed and results in an error during the markup validation process:
It is not the first time I have noticed such behavior. I saw it for the first time a while ago, while I was working on the Imtech SharePoint Compliancy Toolkit to improve accessibility of WCM solutions built on top of MOSS 2007. Because the toolkit uses HTML post processing to clean up the markup it allowed me to remove the second title element in the markup cleaning process.
In the project I’m working on at the moment there is no accessibility requirement which requires the use of the Imtech SharePoint Compliancy Toolkit. We were asked though to deliver an XHTML Transitional valid markup. To find out the reason of the doubled title element I did some research on the page rendering process.
One down one to go
During my research I’ve found that rendering two title elements is caused by not including the title element as a child element of the head section of the page. In the Master Page we were using, the title element was wrapped in a ContentPlaceHolder. It seemed like at some point SharePoint is checking whether the page has a title and if it doesn’t, it adds a new title element to the head section of the page.
Looking a bit further in the rendering process I noticed that it’s not SharePoint that is responsible for the check but ASP.NET! If I’m correct the HtmlHead control checks whether a title element has been defined. If not it creates a new title element. Because the title is wrapped in a ContentPlaceHolder it’s not being detected as a child control of the HtmlHead control and thus the check fails resulting in an additional title element.
The solution to this problem was straight-forward. After removing the ContentPlaceHolder from the head section and adding the title element just below the encoding definition, the second title element disappeared from the output:
Because I was working with MOSS 2007 I suspected it to be the reason of this faulty behavior. It turns out that although SharePoint enriches the rendering process, ASP.NET still has a lot of influence on how the output HTML looks like. Whenever something works not as you would expect it to, it might be helpful to not only look at SharePoint-specific components but those provided with ASP.NET as well.
At some point I compared the Master Page I was using with the Minimal Master Page sample provided on MSDN. Surprisingly the Minimal Master Page has exactly the same issue as it wraps the title element in a ContentPlaceHolder. Understanding the code you are using is a very important part of the troubleshooting process.