如何在数据源位置使用sitecore查询?(动态数据源)

如何在数据源位置使用sitecore查询?(动态数据源),sitecore,sitecore6,Sitecore,Sitecore6,是否可以将数据源位置(而不是数据源)设置为sitecore查询 我试图做的是让子布局将其数据源位置设置为包含它的项(当前项)下的文件夹 子布局数据源位置应指向当前项下的文件夹。因此,我尝试将数据源位置设置为query:./Items/*,但没有成功 您不需要查询——子布局数据源位置可以简单地使用相对路径。e、 g ./Items 显然,该文件夹必须已经存在。我一直想把这段代码写在博客上,这可能有些过分,但我会在这里发布,因为它可能会帮助你。如果相对路径数据源位置不存在,可以将以下内容添加到ge

是否可以将数据源位置(而不是数据源)设置为sitecore查询

我试图做的是让子布局将其数据源位置设置为包含它的项(当前项)下的文件夹

子布局数据源位置应指向当前项下的文件夹。因此,我尝试将数据源位置设置为
query:./Items/*
,但没有成功

您不需要查询——子布局数据源位置可以简单地使用相对路径。e、 g

./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:保存处理程序)。如果您希望在数据源中一般支持相对路径,则可以自定义SublayoutParamHelper实用程序类(或您的版本),以执行Sitecore查询,而不是GetItem。