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

例: ユーザー インターフェイスを表示するコマンド テンプレート コード

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

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

ユーザーがアイテムの挿入時にオプションを選択できるユーザー インターフェイスを表示するコマンド テンプレートを使用できます。

メモ

場合によっては、コマンド・テンプレート・ユーザー・インターフェースをカスタム・アイテム・エディターとして再利用したいことがあります。

コマンド テンプレートの次のコード サンプルは、ユーザーがフォルダーを挿入できるユーザー インターフェイスを示しています。この例は、ユーザー インターフェイスを表示するコマンド テンプレートを実装するために必要な最小限のコンポーネントを示すために、意図的に過度に簡略化されています。

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); 
   }
  }
 }
}

Execute() メソッドに渡される唯一のパラメータはSitecore.Shell.Framework.Commands.CommandContextオブジェクトです。このオブジェクトのItemsコレクションの最初の要素は、ユーザーがコマンド テンプレートを呼び出したときに選択されたSitecore.Data.Items.Itemを識別します。呼び出されたExecuteメソッドは、処理環境に関する情報を準備し、コマンド テンプレート ロジックを含むRun() メソッドを呼び出します。

Run() メソッドは、コマンド テンプレートのユーザー インターフェイス ダイアログ ボックスを呼び出し、ユーザーがそのダイアログ ボックスを完了すると、作成された項目を編集ユーザー インターフェイスに読み込みます。Run()メソッドでは、args.IsPostBackがtrueの場合、ユーザーはインタフェースを完了し、args.Resultに値が含まれている場合は、作成された項目のIDであり、Run()メソッドによってユーザー・インタフェースにその項目がロードされます。args.IsPostBackがfalseの場合、Run() メソッドはコマンド テンプレート ユーザー インターフェイスを呼び出します。

次のコードは、前に示したコード内のRun() メソッドによって参照されるユーザー インターフェイス コンポーネント ファイル (/sitecore modules/shell/Namespace/NewFolder.aspx) の内容を表しています。

<%@ 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>
手記

わかりやすくするために、このコードではASP.NETページを使用しています。Sitecore 8.1より前のバージョンでは、Sitecore Sheerを使用してコマンド テンプレートを実装することもできました。

<base> 要素とその属性は、ユーザーがページ内のボタンをクリックしたときにブラウザーが新しいウィンドウを開くのを防ぎます。

次のコードは、/sitecore modules/shell/Namespace/NewFolder.aspxの分離コードの内容を表しています。

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; 
    }
   }
  }
 }
}
この記事を改善するための提案がある場合は、 お知らせください!