Inconvenient good page title with SharePoint Server Web Content Management (revisited)


One of the things that matter when optimizing an Internet-facing website for search engines is the page title. Next to the level one header (H1) and the URL, the page title is one of the most important elements of your page that allows search engine to find your page. A good page title consists of at least the title of the current page and the title of the website. Additionally, if the site is quite large you can add the name of the section. And although it doesn’t sound like rocket science it is quite inconvenient to do it right in SharePoint.

Anatomy of a good page title

When talking about Page Title I mean the text that you put in the title tag in the head of your page. That title is being displayed on the title bar of the web browser, as a label of the bookmark, in RSS feeds and it’s also being used by search engines in search results. As you can see a good title is pretty important and can really make a difference whether people will visit your page or not.

A good page title should describe what page the visitor is looking at and where this page belongs to. So when thinking in terms of good page titles you might come up with something like this:

Adventure Works Bikes | Super Bike 123

However if you look closer at the title you will find out, that such title is useless. Let me show you why:

Screenshot of favorites in Internet Explorer. Because the title of every favorite begins with 'Adventure Works Bikes' it is difficult to find the right favorite.

First of all every favorite from the whole site starts with Adventure Works Bikes which might be okay the first time you look at the site or if you have only one bookmark from Adventure Works but if you have more of them, it’s going to be difficult to get the right one the first time.

Another thing that you might have noticed is the | (pipe) being removed from the name of the bookmark. Unfortunately the | character isn’t allowed on Windows file names and is therefore being removed, and with it a piece of the meaning of the title.

The improved version of our title might look like this:

Super Bike 123 – Adventure Works Bikes

By reversing the title you make it easier discoverable in the favorites and the title of the page gets more importance when calculating the keywords for the page, increasing the chance of people finding your site when looking for Super Bike 123 instead of Adventure Works Bikes.

Another thing that you benefit of is replacing the separator with a - (dash). Not only is it being included in the name of the bookmark but also it’s being read in a more user-friendly way than the | char. What would like to hear more: Super Bike hundred twenty-three vertical bar Adventure Works Bikes or Super Bike hundred twenty-three dash Adventure Works Bikes?

If your website is divided into clear sections like mountain bikes, gear, etc. which might just be the keywords that people on the Internet might be looking for, you can extend the page title and include the section as well:

Super Bike 123 – Mountain Bikes – Adventure Works Bikes

Adding the section name to the title not only helps you clarify what the page is about but also adds some valuable keywords which might help you increase the ranking of your pages.

Now we know what a good page title should look like let’s try to put it all into a SharePoint solution.

Inconvenient page title revisited

Unfortunately for you SharePoint doesn’t provide any out of the box mechanism that allows you to write good page titles. It also doesn’t have any mechanism that would simplify it, and to make it even worse, writing a good page title isn’t as straightforward as you might it want it to be.

I wrote about the issue with hierarchical page titles before. Back then I showed you a solution for generating a good page title using elevated privileges. While it works, it is bad for many reasons among which performance as you need an elevated instance of the Site Collection and Site. Let’s have a quick look at the issue again.

A good page title consists – in SharePoint terms – of the following pieces:

Page Title – Site Title – Site Collection (Root Web) Title

If you however did just that you would end up with titles like:

Home – Mountain Bikes – Adventure Works Bikes

or

Home – Adventure Works Bikes – Adventure Works Bikes

In order to get the good page titles correctly in SharePoint you have to build-in some intelligence that will allow you to distinct between a subsite and the Root Web and a page and the Welcome Page of your site. And this is where the problems begin.

You can retrieve the title of both the Root and the current site what allows you to distinct between a subsite and the Root Web. However the problems being when you try to retrieve the Welcome Page. As soon as you request the value of the PublishingWeb.DefaultPage property you will get a nice login prompt. This is why we needed an elevated instance of the Site Collection and Site in the solution that I showed you previously.

Looking closer at the DefaultPage property you can find out that all it does is getting the value of the SPWeb.RootFolder.WelcomePage property. Unfortunately you cannot retrieve this value as an anonymous user as well. Even the underlying property from the property bag isn’t available to anonymous users. This leaves us with two possibilities.

AssumeDefaultHomePage

First of all you can assume that every Welcome Page is named default.aspx. By retrieving the page name from the current URL and comparing it to default.aspx you could determine if the current page is a Welcome Page and include its title in the page title if it isn’t. While it seems like a dirty solution, it works pretty fast and it works properly with 99,9% websites.

Elevation

And if you really need to verify if the current page is a Welcome Page? The only thing that you can do to know it for sure is to instantiate an elevated version of the Site Collection and Site and check the value of the DefaultPage property.

bool isHomePage = false;
if (AssumeDefaultHomePage)
{
    isHomePage = Path.GetFileName(SPContext.Current.File.Url).Equals("default.aspx", StringComparison.InvariantCultureIgnoreCase);
}
else
{
    string siteUrl = SPContext.Current.Web.Url;
    string currentPageUrl = SPContext.Current.File.Url;

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite site = new SPSite(siteUrl))
        {
            using (SPWeb web = site.OpenWeb())
            {
                isHomePage = currentPageUrl.Equals(web.RootFolder.WelcomePage, StringComparison.InvariantCultureIgnoreCase);
            }
        }
    });
}

if (!isHomePage)
{
    titleParts.Add(SPContext.Current.ListItem[SPBuiltInFieldId.Title] as string);
}

Summary

Providing a good page title is not only about user-friendliness of the website but can also help you make your content easier to find. Although SharePoint doesn’t provide any mechanism for creating good page titles out of the box, you can easily create one yourself. While developing a control for writing good page titles you should always consider performance and educate your users about the selected solution.

Technorati Tags: SharePoint,WCM,MOSS 2007,SharePoint Server 2010

Others found also helpful: