是否可以将数据源位置(不是数据源)设置为sitecore查询?
我要做的是使子布局将其数据源位置设置为包含它的项(当前项)下的文件夹。
子布局数据源位置应指向当前项目下的文件夹。因此,我尝试将数据源位置设置为query:./Items/*,但不起作用。
发布于 2011-10-07 07:27:36
您不需要查询--子布局数据源位置可以简单地使用相对路径。例如:
./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)));
}
}
}
}
}
}https://stackoverflow.com/questions/7678597
复制相似问题