---
title: "Example: Command template code to display a user interface"
slug: "example--command-template-code-to-display-a-user-interface"
abstract: "Command template code that displays a user interface for inserting a folder."
---
# Example: Command template code to display a user interface

You can use a command template that displays a user interface in which the user can select options when inserting items.

> [!note]
> In some cases, you might want to reuse the command template user interface as a custom item editor.

The following code sample for a command template presents a user interface that lets the user insert a folder. This example is intentionally oversimplified to present the minimal components required to implement a command template that displays a user interface.

```csharp
namespace Namespace.Shell.Framework.Commands
 {
  public class NewFolder : Sitecore.Shell.Framework.Commands.Command
 {
   public override void Execute(Sitecore.Shell.Framework.Commands.CommandContext context)
   {
     if (context.Items.Length == 1)
   {
     Sitecore.Data.Items.Item item = context.Items[0];
     System.Collections.Specialized.NameValueCollection parameters = new
     System.Collections.Specialized.NameValueCollection();
     parameters["id"] = item.ID.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)
  {
    if ((!String.IsNullOrEmpty(args.Result)) && args.Result != "undefined")
  {
    Sitecore.Context.ClientPage.SendMessage(this,"item:load(id="+args.Result +")");
  }
 }
 else
 {
   Sitecore.Text.UrlString url = new Sitecore.Text.UrlString( "/sitecore modules/shell/Namespace/NewFolder.aspx");
   url.Append("id", args.Parameters["id"]);
   url.Append("database", args.Parameters["database"]); Sitecore.Context.ClientPage.ClientResponse.ShowModalDialog(url.ToString(),true);
   args.WaitForPostBack(true);
   }
  }
 }
}

```

The only parameter passed to the `Execute()` method is a `Sitecore.Shell.Framework.Commands.CommandContext` object. The first element in the Items collection of this object identifies the `Sitecore.Data.Items.Item` that was selected when the user invoked the command template. The `Execute` method invoked prepares information about the processing environment, and then invokes the `Run()` method, which contains the command template logic.

The `Run()` method invokes the command template user interface dialog box, and, after the user completes that dialog box, the method loads the created item into the editing user interface. In the `Run()` method, if `args.IsPostBack` is true, then the user has completed the interface, and if `args.Result` contains a value, it is the ID of the item created, and the `Run()` method causes the user interface to load that item. If `args.IsPostBack` is false, the `Run()` method invokes the command template user interface.

The following code represents the content of the user interface component file (`/sitecore modules/shell/Namespace/NewFolder.aspx`), referenced by the `Run()` method in the code provided previously:

```csharp
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NewFolder.aspx.cs" Inherits="Namespace.Web.UI.NewFolder" %>
 <html>
    <head>
    <script language="javascript" type="text/javascript">
 function onClose()
 {
 window.returnValue = form1.hidCreatedId.value;
 window.close();
 }
 function onCancel()
 {
 if(confirm( "Are you sure you want to cancel?"))
 {
window.close();
}
window.event.cancelBubble = true;
 return false;
 }
</script>
 <base target="_self" />
</head>
 <body>
 <form id="form1" runat="server">
 <div>
<asp:textbox ID="txtName" runat="server" /><br />
 <asp:checkbox id="chkCopyInsertOptions" checked="true" runat="server" Text="Copy Insert Options" />
 <asp:checkbox id="chkCopyInsertRules" checked="true" runat="server" Text="Copy Insert Rules" /> <br />
<asp:button runat="server" id="btnCreate" text="Create" />
<asp:button runat="server" id="btnCancel" text="Cancel" onclientclick="onCancel();" />
<asp:button runat="server" id="btnDone" text="Done" onclientclick="onClose();" visible="false"/><br />
 <input type="hidden" runat="server" id="hidCreatedId" /><br />
 </div>
</form>
</body>
 </html>

```

> [!note]
> For clarity, this code uses an ASP.NET page. Prior to Sitecore 8.1, you could also implement command templates using Sitecore Sheer.

The `<base>` element and its attributes prevent the browser from opening a new window when the user clicks a button in the page.

The following code represents the content of the code-behind for `/sitecore modules/shell/Namespace/NewFolder.aspx`:

```csharp
namespace Namespace.Web.UI
{
 public partial class NewFolder : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
 string dbName = Sitecore.Web.WebUtil.GetQueryString("database");
 Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase(dbName);
 string parentID = Sitecore.Web.WebUtil.GetQueryString("id");
 Sitecore.Data.Items.Item parent = db.GetItem(parentID);
 if (String.IsNullOrEmpty(hidCreatedId.Value)
   && (!String.IsNullOrEmpty(txtName.Text) && txtName.Text != "undefined"))
 {
   chkCopyInsertOptions.Enabled = false;
   chkCopyInsertRules.Enabled = false;
   Sitecore.Data.Items.TemplateItem template =
     Sitecore.Context.ContentDatabase.Templates[Sitecore.TemplateIDs.Folder];
   Sitecore.Data.Items.Item item = parent.Add(txtName.Text, template);
   txtName.Enabled = false;
   btnDone.Visible = true; btnCreate.Visible = false;
   btnCancel.Visible = false;
   hidCreatedId.Value = item.ID.ToString();
   if(chkCopyInsertOptions.Checked||chkCopyInsertRules.Checked)
   {
     item.Editing.BeginEdit();
     if (chkCopyInsertOptions.Checked)
     {
       item.Fields[Sitecore.FieldIDs.Branches].Value = parent[Sitecore.FieldIDs.Branches];
     }
     if (chkCopyInsertRules.Checked)
     {
       item.Fields["__insert rules"].Value = parent["__insert rules"];
     }
     item.Editing.EndEdit();
   }
}
else { if(String.IsNullOrEmpty(parent[Sitecore.FieldIDs.Branches]))
   {
   chkCopyInsertOptions.Enabled = false;
   chkCopyInsertOptions.Checked = false;
    }
   if(String.IsNullOrEmpty(parent["__insert rules"]))
    {
     chkCopyInsertRules.Enabled = false;
     chkCopyInsertRules.Checked = false;
    }
   }
  }
 }
}

```