ChangeTemplate with Revolver

Leave a comment

November 13, 2008 by Alistair Deneys

One of the goals of Revolver is to provide a platform for custom tool development and execution. Often we need to execute some code against an arbitrary set of items from the content tree. For example, yesterday I had to change the template of most of the items in the content tree of the site I was working on. And I wasn’t about to click through 200 items changing templates using the content editor. Enter Revolver!

Revolver allows you write your own Revolver commands and bind them into the Revolver shell. Your command must implement the Revolver.Core.ICommand interface. An easy way to do this is to inherit a class from Revolver.Core.Commands.BaseCommand which handles the initialisation stuff to store the Revolver context objects and provide access to them through properties.

So, for my above scenario, I know I could use the Sitecore.Data.Items.Item.ChangeTemplate method. So I will create a custom command to execute this method on the context item in Revolver.

First, create a new class library project in Visual Studio and add references to the Revolver.Core and Sitecore.Kernel assemblies. Now add the appropriate usings to your code file.

using Revolver.Core;
using Revolver.Core.Commands;
using Sitecore.Data.Items;

Next, create a new class which inherits from BaseCommand and implement the abstract methods.

namespace ChangeTemplate
{
  public class Command : BaseCommand
  {
    public override string Description()
    {
      throw new NotImplementedException();
    }

    public override HelpDetails Help()
    {
      throw new NotImplementedException();
    }

    public override CommandResult Run(string[] args)
    {
      throw new NotImplementedException();
    }
  }
}

We need to implement 3 methods. The first is Description which returns a string describing what the command does. The second is Help, which returns a HelpDetails object containing the help information for the command. These methods are called from the help command inside Revolver. Lastly, the Run command is what gets called when we execute our custom command in Revolver. The string array passed to this method are the parameters passed on the command line from the Revolver shell.

So let’s start implementing the help methods.

public override string Description()
{
  return "Change an item's template";
}

public override HelpDetails Help()
{
  HelpDetails details = new HelpDetails();
  details.Description = Description();
  details.Usage = "<cmd> templatePath";
  details.AddParameter("templatePath", "The template to change to");
  details.AddExample("<cmd> (sample/sample item)");
  return details;
}

The Description method is called when the help command is executed without any parameters and the output is displayed in a list of the available commands next to the currently bound command moniker. The Help method is called when the help command is executed and passed the current moniker for the command to get more detailed help information for the command.

Note in the HelpDetails object for usage and example I don’t use any particular command moniker, but instead use the placeholder “<cmd>”. This is because the custom command may be bound to any moniker at all. Fill in as many of the HelpDetails properties as possible to provide the user with enough information to be able to use your command.

And now, onto the Run method. This method returns a Revolver.Core.CommandResult object which indicates the success or failure of the command as well as any messages to pass back to the user.

public override CommandResult Run(string[] args)
{
  if (args.Length < 1)
    return new CommandResult(CommandStatus.Failure,
      "Failed to find required parameter 'templatePath'");

  TemplateItem template = Context.CurrentDatabase.Templates[args[0]];
  if (template == null)
    return new CommandResult(CommandStatus.Failure,
      "Failed to find template " + args[0]);

  Context.CurrentItem.ChangeTemplate(template);
  return new CommandResult(CommandStatus.Success,
    "Changed template to " + args[0]);
}

The Context property is provided by the CommandBase class which took care of storing the Revolver context and format context objects.

Now that my command is implemented I can bind the command into a Revolver shell and execute it against the items required. Firstly, build the code and place the output assembly in the bin folder of the Sitecore site you’re going to use it in. Next, log into Sitecore and open a Revolver shell. We use the bind command to bind the custom command into the shell. Let’s bind the change template command to the moniker “ct”.

bind ChangeTemplate.Command,ChangeTemplate ct

The first parameter provided to bind is the fully qualified type name. The second is the moniker to bind the command to.

Now I can execute the change template command by executing ct and providing the template I wish to change the current item to.

ct (sample/sample item)

But I’m only in a slightly better position than I was at the start of the article. I’m not going to navigate round 200 items in my content tree to execute a command. And this is where the real power of Revolver comes in. I can combine by command with the find or query commands to have it execute against a set of items. In my scenario, I need to change all items which currently have a template of ‘sample item’ and change it to ‘real item’. So I’ll be using the attribute filter on the find command.

find -a templateid {76036F5E-CBCE-46D1-AF0A-4143F9B557AA} (ct (real item))

or

find -a template (/sitecore/templates/Sample/Sample Item) (ct (real item))

And what’s more is I managed to throw the above custom command together and execute it against the content tree within 10 minutes. I just saved myself hours and countless frustration.

Advertisements

Leave a Reply

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Categories

The views expressed on this blog are solely my own and do not necessarily reflect the views of my employer.
%d bloggers like this: