Create a command template
How to 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:namespace 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/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:<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:
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.