Tag: CustomConfiguration

Sitecore Custom Tokens

Out of the box, Sitecore comes with the following expand standard values tokens:

  • $name – Name of the item
  • $date – Current server date
  • $time – Current server time
  • $now – Current server date time
  • $id – Item ID
  • $parentid – Item’s Parent ID
  • $parentname – Item’s Parent Name

Sometimes the already existing tokens are not enough. There are cases in which the Content Editors won`t be able to access the value that needs to be populated (ex. – webservice call) or it will be hard for them to do it (ex. – the value of the item varies on many other items). In the current example, the $next token will be implemented. It will automatically replace the token with the current siblings’ count plus one  – this token is usually used in cases where there is a requirement for auto-incrementing sort order of items.

When it comes down to creating your own custom tokens, there are basically two options to choose from:

  1. Extend the MasterVariablesReplacer class
  2. Extend the ExpandInitialFieldValueProcessor class

The first option involves extending and overriding default functionality of the class, that provides the replacement mechanism for all the standard Sitecore tokens. This approach is explained in John West’s blogpost on how to add custom standard value tokens to Sitecore. It should be noted, that extending from the MasterVariablesReplacer class requires some method overriding. Be aware, that since the class is not really meant for extending in the first place you’ll have to duplicate a great amount of the private members into your own custom implementation.

The other option is the inheritance from the ExpandInitialFieldValueProcessor class, which is my preferred way to create replacement mechanisms for custom tokens in Sitecore. Using the ExpandInitialFieldValueProcessor is really easy, as it only requires you to extend from the base class and implement the void Process(ExpandInitialFieldValueArgs args) method:

namespace AndreyVinda.Processors
    using Sitecore.Pipelines.ExpandInitialFieldValue;

    public class TokenNextProcessor : ExpandInitialFieldValueProcessor
        public override void Process(ExpandInitialFieldValueArgs args)
            if (args.SourceField.Value.Contains("$next"))
                if (args.TargetItem != null && args.TargetItem.Parent != null && args.TargetItem.Children != null)
                    args.Result = args.Result.Replace("$next", args.TargetItem.Parent.Children.Count.ToString());
                    args.Result = args.Result.Replace("$next", "0");

The code is pretty straightforward. It checks if the value of the field contains the $next token. If it does it checks if the parent is not null and the parent has children. If the parent is not null and there are children the value is set to the children`s count (the processor is executed after the child is added so the index will be 1 based), otherwise it sets the value to 0.

Once the token replacer is implemented, all that is left to do is to patch in the processor into the Sitecore pipeline config section for the expandInitialFieldValue pipeline:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
        <processor type="AndreyVinda.Processors.TokenNextProcessor , AndreyVinda.Processors" patch:after="processor[@type='type=Sitecore.Pipelines.ExpandInitialFieldValue.ReplaceVariables, Sitecore.Kernel']"/>