Sitecore Corner

Sitecore Tips and Tricks


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());
                }
                else
                {
                    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="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <expandInitialFieldValue>
        <processor type="Sandbox.Processors.TokenNextProcessor , Sandbox" patch:after="processor[@type='type=Sitecore.Pipelines.ExpandInitialFieldValue.ReplaceVariables, Sitecore.Kernel']"/>
      </expandInitialFieldValue>
    </pipelines>
  </sitecore>
</configuration>

And here is an example of how it works.

Adding The Token to the standard values

The content tree

The result

 



7 responses to “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.

    Like

    1. Hi Mike !

      Thank you for pointing out the different implementation approach !

      Regards,
      Nikola

      Like

  2. Hi Nikola,
    Good idea 🙂

    Have you seen my Sitecore Extension Project for Tokens? I’d like to include your token idea if that’s ok for you.

    Regards,
    Reto

    Like

    1. Hi Reto.

      No problems at all ! Feel free to add it !
      Regards,
      Nikola

      Like

  3. […] a number of blog posts about creating custom Sitecore tokens. The one that I found most helpful was this straightforward post from SitecoreCorner that explains simply how to create the right type of resolver to process […]

    Like

Leave a comment

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