$name in your Sitecore content

One issue that seems to regularly occur in Sitecore builds is pages showing $name in the content. Its the sort of problem that seems to creep into projects the further through you get.

Why does it happen?

When you setup Sitecore templates they are typically built up from several other templates eg:

  • A content page is comprised from:
    • Page Title field section
    • Meta Data field section
    • etc

Down the line you decide you want to add ‘Page Content – (Header and Body fields)’ to all pages so you setup a new Field Section template along with corresponding fields and __standard values. So that new pages automatically push the item name to the Header field, you set the __standard values to contain $name.

All this gets published and you then come to check the front end of the site – pages created before adding the Page Content field section now have the new header and body fields but the header field shows $name ๐Ÿ™

The reason being these fields are inheriting their value from __standard values – variables such as $name are processed when items are created. See http://adeneys.wordpress.com/2009/12/29/custom-tokens-and-nvelocity-for-item-creation/ for info on how to create custom replacement tokens. There is also more information on http://learnsitecore.cmsuniverse.net/en/Developers/Articles/2010/08/Standard-values-in-sitecore.aspx

How to solve the problem?

When items are created they are processed by a set of pipelines. An example of this is expandInitialFieldValue pipeline. Out the box, this makes use ofย  the MasterVariablesReplacer.

You now have a couple options, either you can call the MasterVariablesReplacer for the problematic items or you could manually script the same functionality. The following code demonstrates the manual approach:

Its worth noting this can introduce some interesting challenges if Language Fallback is used on your site. The script above works fine for content items on a single language site. If you need similar functionality when language fallback is in place it may actually be meaningful to set $name = ” for non-primary languages. This will ensure the fallback will occur correctly, rather than finding $name and thinking it is valid content.

3 thoughts on “$name in your Sitecore content

  1. Thanks for the post. I didn’t check the code very closely, but couldn’t other tokens than $name appear, such as $id? Couldn’t $name appear in the middle of the field? Is there any chance that $name could appear in item names (such as those created from branch templates), or only in field values? I wonder if you could use the class specified by the MasterVariablesReplacer setting in the web.config file, maybe through the Sitecore.Configuration.Factory.GetMasterVariablesReplacer() method, to handle more cases with less code.

    • Cheers for the feedback! You are correct, there are other tokens which this wouldn’t catch. In previous projects I’ve never needed to use any tokens apart from $name. To catch all, the expandInitialFieldValue pipeline or the MasterVariablesReplacer would be better option.

      I don’t think you could end up with $name in the name of an item after template inheritance is changed. The reason being an items name isn’t related to data stored in its template or __standard values, instead it’s setup when an item is created. Fields differ since they can be added to an item over time.

      Please correct me if I’m wrong!!!! ๐Ÿ™‚

  2. Hi there,

    I’ve also experienced this issue where all our contents were displaying $name. The root cause is as follow:

    1. The item does not have a version.
    2. Template is version in other language.
    3. Invarient Language is present in the Language list.

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *