Sitecore Best Practice #9 - Spaces are for people, dashes are for browsers

26 August 2011
Adam Najmanowicz
This Best Practice describes how to maintain an elegant, consistent and system-enforced URL strategy for pages in Sitecore, with implementation guidelines.

One of the best features a modern CMS provides is the ability to maintain a clean and consistent URL strategy. While Sitecore gives you (the developer) the possibility – by default it does not enforce any naming policy. It will however warn the editor with a message (as shown below) that a name can potentially cause your URLs to look less appealing than what you might like:

If your editor is familiar with SEO principles – then good for you. However, if you want the validation to go away ,you could run into a scenario where your tree has the names with Dashes or have the URLs camel cased (in our case, Sitecore does not like a space in the name).

In my opinion, a system where the name is enforced and consistent with the strategy that’s established during the project design is a preferred scenario as it frees your author from NOT having to think about it. Happier authors - Happier customers – Higher ROI!

What is it about?

Items should have descriptive names in the Content Editor tree, while the URLs should reflect the same name with spaces. This means that in the Content Editor, authors use 'Standard Title Capitalization' and spaces, while the URLs the browser understands are all lower case and contain hyphens instead of spaces.

Why should I do it?

Sitecore has two special item fields that are relevant to this scenario : Name and DisplayName. The first one is used for generating item links and URLs, while the second is shown to the authors. However, DisplayName is empty by default (it can be set from Home tab, on Rename chunk - Dsplay Name button), and in such case, Name is displayed. This document describes how an automatic event handler can be attached to Sitecore that enforces the preferred item naming conventions while still showing the authors the display names they have chosen.

Demo scenario

Our URL strategy states the following as we give more weightage to how Google handles spaces and dashes over Bing:
  • URLs have spaces replaced with dashes
  • URLs are all lowercase
How the editor sees it:  

How do I implement it in my project?

Implement your class as follows:
public class ItemEventHandler
    protected void HandleItemName(object sender, EventArgs args)
        var item = (Item)Event.ExtractParameter(args, 0);
        string processedName;

        if (item.Database.Name != "master"
            || !item.Paths.Path.StartsWith("/sitecore/content/Home/")
            || item.Name == (processedName = item.Name.ToLower().Replace(' ', '-')))

            item.Appearance.DisplayName = item.Name;
            item.Name = processedName;
And enable the ItemHandler for item:added and item:renamed notifications. This requires adding it in two places in the web.config:
<event name="item:added">
  <handler type="MyNameSpace.ItemEventHandler, MyAssembly" method="HandleItemName"/>
<event name="item:renamed">
  <handler type="MyNameSpace.ItemEventHandler, MyAssembly" method="HandleItemName"/>
Simple enough... Easy to put a smile on your editor's faces, isn't it?

Thanks to Leszek Ciesielski for his help and skills with the Sitecore Best Practice #9.