Architecture, Sitecore

Command templates – the forgotten feature of Sitecore 6?


I don’t know about you, but I actually haven’t been giving command templates any attention since they were introduced in Sitecore 6. This is really a shame, as it is pretty powerful and can help editors a lot.

For instance take the often used structure for news, where you have a news folder. To avoid too many items in a flat structure you have folders for the current year and the current month. This could back in the old days be used by letting the editors them self create the folders they needed or you could have implemented a lot of event handlers or similar. Now you can just use a command template and let the command create the folders from the current DateTime.

To create command templates follow the guide in the data definition cookbook . Then implement the command to create the folders like this:

public class MyCustomCommand : Command
{
  public override void Execute(CommandContext context)
  {
  if (context.Items.Length == 1)
  {
  Item item = context.Items[0];
  NameValueCollection parameters = new NameValueCollection();
  parameters["id"] = item.ID.ToString();
  parameters["language"] = item.Language.ToString();
  parameters["database"] = item.Database.Name;
  Sitecore.Context.ClientPage.Start(this, "Run", parameters);
  }
}


protected void Run(Sitecore.Web.UI.Sheer.ClientPipelineArgs args)
{
  if (!args.IsPostBack)
  {
    //Means we are in the initial step, we want to ask for the name of the news
    Sitecore.Context.ClientPage.ClientResponse.Input("Enter the name of the news item:", Sitecore.Globalization.Translate.Text("New NewsItem"), Sitecore.Configuration.Settings.ItemNameValidation, "'$Input' is not a valid name.", 100);
    args.WaitForPostBack();
  }
  else
  {
    //Now we got a postback, which means we got a response
    if (!String.IsNullOrEmpty(args.Result) && args.Result != "undefined")
    {
      Database db = Sitecore.Configuration.Factory.GetDatabase(args.Parameters["database"]);
      Item parent = db.GetItem(args.Parameters["id"], Language.Parse(args.Parameters["language"]));
      //Create the folder structure if it doesn't exist
      string year = DateTime.Now.Year.ToString();
      string month = DateTime.Now.Month.ToString();
      TemplateItem folderTemplate = db.Templates[Sitecore.TemplateIDs.Folder];
      Item yearFolder = parent.Children[year];
      if (yearFolder == null)
        yearFolder = parent.Add(year, folderTemplate);
      Item monthFolder = yearFolder.Children[month];
      if (monthFolder == null)
        monthFolder = yearFolder.Add(month, folderTemplate);
      //Create the news Item
      TemplateItem template = Sitecore.Context.ContentDatabase.Templates[new ID("{B2612CF6-16D6-426D-9E74-EE3A4E3989B2}")];
      Item item = monthFolder.Add(args.Result, template);
      //Load the Item in the content editor
      Sitecore.Context.ClientPage.SendMessage(this, "item:load(id=" + item.ID.ToString() + ")");
    }
  }
 }
}

There you go – auto creation of folders. It’s that simple. I can think of really many usages of this – also more complex situations. Too bad the sheer UI documentation is so limited, as it makes it difficult to implement complex wizards etc.

Standard

6 thoughts on “Command templates – the forgotten feature of Sitecore 6?

  1. Commodore73 says:

    There is some information about command templates in the Client Configuration resources on SDN. There’s a related discussion here:

    http://sdn.sitecore.net/Forum/ShowPost.aspx?PostID=16200

    I realize it probably works, but I don’t like code like this:

    if (!String.IsNullOrEmpty(args.Result) && args.Result != “undefined”)

    I know mathematically it might, but grammatically it doesn’t indicate to which condition the first not (!) applies.

  2. Jens Mikkelsen says:

    Well this is only example code, so it hasn’t been given any attention really. It is more or less copied from the Data Definition cookbook, so feel free to alter it and create a readable and architectual correct implementation.
    For instance the folder creation should probably be abstracted out of the method.

    The post was more to give command templates some attention, and because I just realised the possibilities of the concept and hoped to pass it on to others.

    Cheers

  3. Raul Jimenez says:

    Jens, I agree with you this is a great feature. In the content editor it works very well, have you tried it from the page editor? It prompts twice for the name, once for normal page editor functionality and second time because of the command template code.
    Solution is simple though, as fortunately the result of the first prompt is sent to the command template as context.Parameters[“name”]…
    Next problem is that it does not redirect to the right page, still figuring out that one though.

  4. James says:

    Hi,

    I have a command template and am using

    if (!String.IsNullOrEmpty(args.Result) && args.Result != “undefined”)

    to check if there is a Result. However I have looked more closely at it and what is coming back when there is no result (args.HasResult == false) is a string whose value is “null”, that is, not NULL but “null”! Is this for real or have I just missed something? I have changed my code for the moment to check the value of args.HasResult instead. I am using Sitecore 6.2 (rev. 091012)

    Cheers,

    James.

Leave a comment