Command template define a class and method to be called
during an insert operation. Unlike data templates and branch templates, which
consist of predefined structures, command templates reference Sitecore UI
commands to invoke wizards or other logic used to create new items.
We can create a command template that displays no user
interface, a command template that uses a JavaScript prompt to collect an item
name, or a command template that displays an ASP.NET or Sitecore Sheer user
interface.
We can assign command templates along with data templates
and branch templates using insert
options.
Following example demonstrates creation of a command
template which would check if a folder with current year name exists under main
item “News” and add a “News Item” in
that folder. This example uses a JavaScript prompt to collect News Item name.
Step 1: Create
two templates “News” & “News Item” with required fields.
Step 2: Create a
class that inherits from Sitecore.Shell.Framework.Commands.Command,
and override the Execute() method.
namespace
Demo.Commands
{
public class NewsItemCommand : Sitecore.Shell.Framework.Commands.Command
{
public override void
Execute(Sitecore.Shell.Framework.Commands.CommandContext context)
{
}
}
}
Step 3: Add a
/configuration/command element to the file /App_Config/Commands.config.
Step 4: Under the
appropriate project-specific folder within /sitecore/Templates,
insert a command template definition item using /System/Branches/Command Template data template.
Step 5: In the
command template definition item, in the Data section, for the Command field,
enter the command code.
The parameter id=$ParentID passes the ID of the item under
which the user is inserting a new item. Without this parameter, Sitecore would
pass the ID of the selected item rather than the item the user right-clicked.
Step 6: Assign
this command template to standard values of the “News” template created in
step1.
Step 7: Create a
new aspx page in “/sitecore modules/shell”
Step 8: Complete
required code in the command class.
public
override void Execute(Sitecore.Shell.Framework.Commands.CommandContext context)
{
if (context.Items != null
&& context.Items.Length > 0)
{
Item contextItem =
context.Items[0];
NameValueCollection parameters
= new NameValueCollection();
parameters["id"] =
contextItem.ID.ToString();
parameters["name"] =
contextItem.Name;
parameters["database"] = contextItem.Database.Name;
parameters["language"] = contextItem.Language.ToString();
Sitecore.Context.ClientPage.Start(this,"Run",
parameters);//Run method executes on
}
}
protected void
Run(Sitecore.Web.UI.Sheer.ClientPipelineArgs args)
{
if (args.IsPostBack)
{
if
(!(String.IsNullOrEmpty(args.Result)) && (args.Result !=
"undefined") && (args.Result != "null"))
{
}
}
else
{
Sitecore.Text.UrlString url =
new Sitecore.Text.UrlString("/sitecore
modules/shell/Demo/News.aspx");
url.Append("id",
args.Parameters["id"]);
url.Append("database", args.Parameters["database"]);
Sitecore.Context.ClientPage.ClientResponse.ShowModalDialog(url.ToString(),
true);
args.WaitForPostBack(true);
}
}
Step 9: Handle
“OK” and “Cancel” buttons of the user interface News.aspx
protected
void btnOK_Click(object sender, EventArgs e)
{
if (!
String.IsNullOrEmpty(txtNewsItem.Text))
{
Sitecore.Data.Database
currentDatabase =
Sitecore.Configuration.Factory.GetDatabase(Sitecore.Web.WebUtil.GetQueryString("database"));
Item parentItem =
currentDatabase.GetItem(Sitecore.Web.WebUtil.GetQueryString("id"));
if (parentItem != null)
{
//check if News item
contains a folder with current year
Item[] children = parentItem.Axes.GetDescendants();
Item currentYearItem =
children.Where(x => x.TemplateName == "Folder" && x.Name
== DateTime.Now.Year.ToString()).SingleOrDefault();
if (currentYearItem ==
null)
{
Sitecore.Data.Items.TemplateItem
folderTemplate = currentDatabase.GetTemplate(Sitecore.TemplateIDs.Folder);
if (folderTemplate !=
null)
{
Item i =
parentItem.Add(DateTime.Now.Year.ToString(), folderTemplate);
currentYearItem =
i;
}
}
//create a news item and
add it to the current folder
Sitecore.Data.Items.TemplateItem
newsItemTemplate = currentDatabase.GetTemplate(new
Sitecore.Data.ID("{D8BE36D8-20F0-4BCF-856C-D069E845D136}"));
Item newsItem =
currentYearItem.Add(txtNewsItem.Text, newsItemTemplate);
ScriptManager.RegisterClientScriptBlock(this,
this.GetType(), "Close", "window.top.dialogClose();", true);
}
}
else
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
"Warning", "alert('Please enter news item');", true);
}
}
protected void btnCancel_Click(object
sender, EventArgs e)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
"Close", "window.top.dialogClose();", true);
}
Step 10: Create a
main news item based on “News” template.
Step 11: Create a
news item under main item by clicking the command template which would create a
folder with current year and a “News Item” under that folder.


































