Home » Commands » Expand Tokens on Sitecore Items Using a Custom Command in Revolver

Expand Tokens on Sitecore Items Using a Custom Command in Revolver

Sitecore Technology MVP 2016
Sitecore MVP 2015
Sitecore MVP 2014

Enter your email address to follow this blog and receive notifications of new posts by email.

On September 18, 2013, I presented Sitecore from the Command Line at the Sitecore User Group – New England.

During my presentation, I gave an example of creating a custom command in Revolver — the first scripting platform for Sitecore built by Alistair Deneys — and thought I would write something up for those who had missed the presentation, or wanted to revisit what I had shown.

One thing that plagues some Sitecore developers — if you disagree please leave a comment — is not having a nice way to expand tokens on items when tokens are added to Standard Values after items had been created previously.

Newly added tokens “bleed” into preexisting items’ fields, and I’ve seen developers perform crazy feats of acrobatic gymnastics to expand them — writing a standalone web form to recursive crawl the content tree to expand these is such an example (take a look at Empower Your Content Authors to Expand Standard Values Tokens in the Sitecore Client where I offer an alternative way to expand tokens on content items).

The following custom Revolver command will expand tokens on a supplied Sitecore item, and help out on the front of expanding newly added tokens on preexisting items:

using System;
using Sitecore.Configuration;
using System.Linq;

using Sitecore.Data;
using Sitecore.Data.Items;

using Revolver.Core;
using Revolver.Core.Commands;

namespace CommandLineExtensions.Revolver.Commands
    public class ExpandTokensCommand : BaseCommand
        private static readonly MasterVariablesReplacer TokenReplacer = Factory.GetMasterVariablesReplacer();

        public override string Description()
            return "Expand tokens on an item";

        public override HelpDetails Help()
            HelpDetails details = new HelpDetails
                Description = Description(),
                Usage = "<cmd> [path]"
            details.AddExample("<cmd> /item1/item2");
            return details;

        public override CommandResult Run(string[] args)
            string path = string.Empty;
            if (args.Any())
                path = args.FirstOrDefault();

            using (new ContextSwitcher(Context, path))
                if (!Context.LastGoodPath.EndsWith(path, StringComparison.CurrentCultureIgnoreCase))
                    return new CommandResult
                        string.Format("Failed to expand tokens on item {0}\nReason:\n\n An item does not exist at that location!", path)

                CommandResult result;
                Item item = Context.CurrentItem;
                    result = new CommandResult(CommandStatus.Success, string.Concat("Expanded tokens on item ", Context.LastGoodPath));
                catch (Exception ex)
                    result = new CommandResult(CommandStatus.Failure, string.Format("Failed to expand tokens on item {0}\nReason:\n\n{1}", path, ex));

                return result;

Tokens are expanded using an instance of the Sitecore.Data.MasterVariablesReplacer class — you can roll your own, and wire it up in the “MasterVariablesReplacer” setting of your Sitecore instance’s Web.config — which is provided by Sitecore.Configuration.Factory.GetMasterVariablesReplacer().

All custom commands in Revolver must implement the Revolver.Core.ICommand interface. I subclassed Revolver.Core.Commands.BaseCommand — which does implement this interface — since it seemed like the right thing to do given that all “out of the box” commands I saw in Revolver were subclassing it, and then implemented the Description(), Help() and Run() abstract methods.

I then had to bind the custom command to a new name — I chose “et” for “Expand Tokens”:


bind CommandLineExtensions.Revolver.Commands.ExpandTokensCommand,CommandLineExtensions et


Since it wouldn’t be efficient to type and run this bind script every time I want to use the “et” command, I added it into a startup script in the core database:


I then had to create a user script for the startup script to run. I chose the Everyone role here for demonstration purposes:


The above startup script will be invoked when Revolver is opened, and our custom command will be bound.

Let’s see all of the above in action.

I added some tokens in my home item:


I then opened up Revolver, navigated to /sitecore/content, and ran the custom command on the home item:


As you can see the tokens were expanded:


You might be thinking “that’s wonderful Mike — except now I have to navigate to every item in my content tree using Revolver, and then run this custom command on it”.

Well, I do have a solution for this: a custom script that grabs an item and all of its descendants using a Sitecore query, and passes them to the custom command to expand tokens:


if ($1$ = \$1\$) (exit (Missing required parameter path))


query -ns $1$/descendant-or-self::* et

I put this script in the core database, and named it “etr” for “Expand Tokens Recursively”:


I navigated to a descendant of /sitecore/content/home, and see that it has some unexpanded tokens on it:


I went back to Revolver, and ran the “etr” command on the home item:


As you can see tokens were expanded on the descendant item:


If you have any thoughts on this, or have ideas for other custom commands in Revolver, please share in a comment.


1 Comment

  1. […] Expand Tokens on Sitecore Items Using a Custom Command in Revolver […]


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

WordPress.com Logo

You are commenting using your WordPress.com 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: