Create a command template

Command templates initiate commands to create one or more items programmatically.

To create a command template:

  1. Create a class that inherits from Sitecore.Shell.Framework.Commands.Command, and override the Execute() method as illustrated in the following code sample:

    RequestResponse
    namespace Namespace.Shell.Framework.Commands
     {
         public class ClassName : Sitecore.Shell.Framework.Commands.Command 
         {
             public override void Execute(Sitecore.Shell.Framework.Commands.CommandContext context)
            {
            }
         } 
    }
  2. Add a /configuration/command element to the /App_Config/Commands.config file, mapping the command code in the name attribute to the class signature in the type attribute. For example:

    RequestResponse
    <command name="namespace:category:command" type="Namespace.Shell.Framework.Commands.ClassName,Assembly" />
  3. In the Template Manager or the Content Editor, within /Sitecore/Templates, insert any required project-specific folders using the /Templates/Common/Folder data template.

  4. Under the relevant project-specific folder within /Sitecore/Templates, insert a command template definition item using the /System/Branches/Command Template data template.

  5. In the command template definition item, in the Data section, for the Command field, enter the command code. For example:

    RequestResponse
    namespace:category:command(id=$ParentID)

    The parameter id=$ParentID sets the ID passed to the command to the item under which the user is inserting a new item. This causes Sitecore to pass the correct item ID to the command template when the user has selected an item, but has right-clicked another item before activating the command template. Without this parameter, Sitecore would pass the ID of the selected item rather than the item that the user right-clicked.

Note

You can assign or copy insert options to other items to allow the use of the new command template.

Do you have some feedback for us?

If you have suggestions for improving this article,