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.

Implementing a custom token is fairly simple and can be achieved in 2 easy steps.

Step 1 – Creating a custom ExpandInitialFieldValueProcessor

Create a custom ExpandInitialFieldValueProcessor with the following code.

namespace Sandbox.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.

Step 2 – Creating the custom configuration

The configuration is standard. It should register the processor under the expandInitialFieldValue pipeline.

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

And here is an example of how it works.

Adding The Token to the standard values

The content tree

The result


6 thoughts on “Sitecore Custom Tokens”

  1. Great post!

    You can also add custom tokens by subclassing Sitecore.Data.MasterVariablesReplacer in Sitecore.Kernel.dll — the <expandInitialFieldValue> pipeline leverages an instance of this object — and override ReplaceField(Item, Field) and Replace(String, Item) — you would have to add your own custom token logic into these methods, and then delegate to the ReplaceField(Item, Field) and Replace(String, Item) methods of the base Sitecore.Data.MasterVariablesReplacer class in order to preserve the “out of the box” functionality.

    You would then have to replace the value of the value attribute of /configuration/sitecore/settings/setting[@name=”MasterVariablesReplacer”] in your Sitecore configuration.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: