1. コマンドテンプレート

例:親アイテムから挿入オプションをコピーするコマンドテンプレートコード

日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

以下のコード例では、ユーザーがフォルダを作成すると、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.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.",
                    Sitecore.Configuration.Settings.MaxItemNameLength);
            }
        }
    }
}

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

この記事を改善するための提案がある場合は、 お知らせください!