Expand Standard Values Tokens in Existing Items with the Sitecore ASP.NET CMS

The issue with resetting standard values and token thread on the Sitecore Developer Network (SDN) forums prompted me to write this blog post that explains how to replace standard values tokens in existing items in the Sitecore ASP.NET web Content Management System (CMS). For more information about standard values, see The Sitecore Data Definition Reference. For more information about standard values tokens, see The Sitecore Data Definition Cookbook.

When you create an item, Sitecore expands tokens in standard field values for that item and updates the item you created accordingly. For example, Sitecore replaces an instances of the token $name in the fields of the item the data template with the name of the item. The supported tokens are:

  • $name: The name of the item
  • $id: The ID of the item
  • $parentid: The ID of the parent of the item
  • $parentname: The name of the parent of the item
  • $date: The system date (yyyyMMdd)
  • $time: The system time (HHmmss)
  • $now: The date and time (yyyyMMddTHHmmss)

If you reset a field to its standard value, Sitecore does not replace these tokens, so they can appear in the field value. To address this issue, I wrote a rules engine action to replace the tokens in field values. This solution is intended for use with the solution described in the blog post Apply Rules to All Items in the Sitecore ASP.NET CMS. This solution invokes the rules engine against the items you specify, and I assume you would use it with the "where true (actions always execute)" condition. For more information about the rules engine, see The Sitecore Rules Engine Cookbook and the blog post All About the Sitecore ASP.NET CMS Rules Engine.

This solution replaces standard values tokens with the appropriate values at the time of replacement. For example, if you have renamed the item since you created it, this solution replaces $name with the current name for the item, not the original name, $parentid and $parentname with information about the current parent, and $date, $time, and $now with the current date and time, not the date and time at the item's creation. The action does not expand standard values tokens in standard values definition items or branch template definition items.

Download the untested prototype, which contains the following files:

  • /Website/Rules/Actions/ExpandStandardValuesTokens.cs: Action implementation class
  • /Data/serialization/master/sitecore/system/Settings/Rules/Common/Actions/Expand Standard Values Tokens.item: Serialization file for action definition item (for more information about serialization, see The Sitecore Serialization Guide)

A more immediate solution would expand standard values tokens immediately after resetting a field. You could override the Reset Fields dialog to include this logic, but that would not handle field resets through API methods (specifically the Sitecore.Data.Fields.Field.Reset() method). A better solution might use an item:saved event handler, but I’ve written enough about event handlers, I wanted to reuse the rules engine infrastructure I developed yesterday, and by the time you discover that you have this issue, you probably want to run this logic against an entire branch of items). For more information about events, see the blog post All About Events in the Sitecore ASP.NET CMS. It should be relatively easy to port this code to an event handler, in which case you should factor the reset logic into a helper class that both the action and the handler can invoke. If you implement that solution, please attach the code and Web.config include file to the SDN forum thread linked above and comment on this blog post. For more information about Web.config include files, see the blog post All About web config Include Files with the Sitecore ASP.NET CMS.