Create a command template
Command templates initiate commands to create one or more items programmatically.
To create a command template:
-
Create a class that inherits from
Sitecore.Shell.Framework.Commands.Command, and override theExecute()method as illustrated in the following code sample:RequestResponsenamespace Namespace.Shell.Framework.Commands { public class ClassName : Sitecore.Shell.Framework.Commands.Command { public override void Execute(Sitecore.Shell.Framework.Commands.CommandContext context) { } } } -
Add a
/configuration/commandelement to the/App_Config/Commands.configfile, 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" /> -
In the Template Manager or the Content Editor, within /Sitecore/Templates, insert any required project-specific folders using the /Templates/Common/Folder data template.
-
Under the relevant project-specific folder within /Sitecore/Templates, insert a command template definition item using the
/System/Branches/Command Template data template. -
In the command template definition item, in the Data section, for the Command field, enter the command code. For example:
RequestResponsenamespace:category:command(id=$ParentID)The parameter
id=$ParentIDsets 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.
You can assign or copy insert options to other items to allow the use of the new command template.