是否可以将数据源位置(而不是数据源)设置为sitecore查询?
我要做的是让sublayout将其数据源位置设置为包含它的项目下的文件夹(当前项).
子布局数据源位置应指向当前项下的文件夹.所以我尝试将数据源位置设置为query:./Items/*但不起作用.
tec*_*414 10
您不需要查询 - 子布局数据源位置可以简单地使用相对路径.例如
./Items
Run Code Online (Sandbox Code Playgroud)
显然,该文件夹需要已经存在.我一直想写博客这个代码,它可能有点矫枉过正,但我会发布在这里,因为它可能对你有帮助.如果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)));
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5034 次 |
| 最近记录: |