---
title: "例:親アイテムから挿入オプションをコピーするコマンドテンプレートコード"
slug: "example--command-template-code-to-copy-insert-options-from-the-parent-item"
abstract: "親アイテムから新しいフォルダに挿入オプションをコピーするコマンドテンプレートコード。"
---
# 例:親アイテムから挿入オプションをコピーするコマンドテンプレートコード

以下のコード例では、ユーザーがフォルダを作成すると、Sitecoreは親アイテムから新しいフォルダに挿入オプションをコピーします。

using System;

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.Items0;
 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.TemplatesSitecore.TemplateIDs.Folder;

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

 if (!(String.IsNullOrEmpty(parentSitecore.FieldIDs.Branches) && String.IsNullOrEmpty(parent" Insert Rules")))
 {
 item.Editing.BeginEdit();
 if (!String.IsNullOrEmpty(parentSitecore.FieldIDs.Branches))
 {
 item.FieldsSitecore.FieldIDs.Branches.Value = parentSitecore.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.",
 Sitecore.Configuration.Settings.MaxItemNameLength);
 }
 }
 }
}

ユーザーがこのコマンドテンプレートを有効にすると、Sitecoreは処理コンテキストに関する情報を準備し、Run()メソッドに渡すExecute()メソッドを呼び出します。Run()メソッドはユーザーにアイテム名を促し、その名前でフォルダを作成し、親アイテムから新しいアイテムに挿入オプションをコピーします。