Example: Command template code to copy insert options from the parent item

Abstract

Command template code that copies the insert options from the parent item to a new folder.

With the following code example, when a user creates a folder, Sitecore copies the insert options from the parent item to the new folder.

namespace Namespace.Shell.Framework.Commands
{
  public class NewAssignedFolder : 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["language"] = item.Language.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.Data.Database db = Sitecore.Configuration.Factory.GetDatabase( args.Parameters["database"]);
   Sitecore.Data.Items.Item parent = db.GetItem(args.Parameters["id"],    Sitecore.Globalization.Language.Parse(args.Parameters["language"]));

Sitecore.Data.Items.TemplateItem template = Sitecore.Context.ContentDatabase.Templates[Sitecore.TemplateIDs.Folder];

Sitecore.Data.Items.Item item = parent.Add(args.Result, template);

if (!(String.IsNullOrEmpty(parent[Sitecore.FieldIDs.Branches]) && String.IsNullOrEmpty(parent[" Insert Rules"])))
{
  item.Editing.BeginEdit();
  if (!String.IsNullOrEmpty(parent[Sitecore.FieldIDs.Branches]))
  {
   item.Fields[Sitecore.FieldIDs.Branches].Value = parent[Sitecore.FieldIDs.Branches];
  }
   if (!String.IsNullOrEmpty(item[" Insert Rules"]))
   {
    item.Fields[" Insert Rules"].Value = parent[" Insert Rules"];
   }
  item.Editing.EndEdit();
  }
  Sitecore.Context.ClientPage.SendMessage(this, "item:load(id=" + item.ID.ToString() + ")");
  }
}
  else
{
Sitecore.Context.ClientPage.ClientResponse.Input( "Enter the name of the new folder:", Sitecore.Globalization.Translate.Text("New folder"), Sitecore.Configuration.Settings.ItemNameValidation, "'$Input' is not a valid name.",

When the user activates this command template, Sitecore invokes the Execute() method, which prepares information about the processing context and passes it to the Run() method. The Run() method prompts the user for an item name, then creates a folder by that name, and copies insert options from the parent item to the new item.