クエリは必要ありません。サブレイアウトデータソースの場所は、単に相対パスを使用できます。例えば
./Items
明らかに、そのフォルダは既に存在する必要があります。私はこのコードをブログに書くことを意味してきましたが、それは残酷かもしれませんが、あなたに役立つかもしれないので、私はここに投稿します。 getRenderingDatasource
パイプラインに以下を追加して、相対パスのデータソースの場所がまだ存在しない場合は作成することができます。プロセッサーGetDatasourceLocation
の前に追加してください。
サブレイアウトでは、作成するアイテムのテンプレートを指定するパラメータcontentFolderTemplate=[GUID]
を追加します。
public class CreateContentFolder
{
protected const string CONTENT_FOLDER_TEMPLATE_PARAM = "contentFolderTemplate";
public void Process(GetRenderingDatasourceArgs args)
{
Assert.IsNotNull(args, "args");
Sitecore.Data.Items.RenderingItem rendering = new Sitecore.Data.Items.RenderingItem(args.RenderingItem);
UrlString urlString = new UrlString(rendering.Parameters);
var contentFolder = urlString.Parameters[CONTENT_FOLDER_TEMPLATE_PARAM];
if (string.IsNullOrEmpty(contentFolder))
{
return;
}
if (!ID.IsID(contentFolder))
{
Log.Warn(string.Format("{0} for Rendering {1} contains improperly formatted ID: {2}", CONTENT_FOLDER_TEMPLATE_PARAM, args.RenderingItem.Name, contentFolder), this);
return;
}
string text = args.RenderingItem["Datasource Location"];
if (!string.IsNullOrEmpty(text))
{
if (text.StartsWith("./") && !string.IsNullOrEmpty(args.ContextItemPath))
{
var itemPath = args.ContextItemPath + text.Remove(0, 1);
var item = args.ContentDatabase.GetItem(itemPath);
var contextItem = args.ContentDatabase.GetItem(args.ContextItemPath);
if (item == null && contextItem != null)
{
string itemName = text.Remove(0, 2);
//if we create an item in the current site context, the WebEditRibbonForm will see an ItemSaved event and think it needs to reload the page
using (new SiteContextSwitcher(SiteContextFactory.GetSiteContext("system")))
{
contextItem.Add(itemName, new TemplateID(ID.Parse(contentFolder)));
}
}
}
}
}
}
これは私が探していたものです。詳細な回答に感謝します。 –
@ techphoria414サブレイアウトデータソースの相対パス設定をサポートするために使用していたSitecoreのバージョンは何ですか?これは私がいくつかのプロジェクトでやってみたいことでした... –
6.4.1、これはページレイアウトエディタで使用されるサブレイアウトのデータソースの場所です。アイテムが選択された後も、それは絶対パスです(項目のGUIDに変更:ハンドラの保存)。一般的にデータソースの相対パスをサポートしたい場合は、GetItemの代わりにSitecoreクエリを実行するようにSublayoutParamHelperユーティリティクラス(またはそのバージョン)をカスタマイズすることができます。 – techphoria414