如何在数据源位置使用Sitecore查询?(动态数据源) [英] How to use sitecore query in datasource location? (dynamic datasource)

查看:36
本文介绍了如何在数据源位置使用Sitecore查询?(动态数据源)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

推荐答案

您不需要查询--子布局数据源位置只需使用相对路径。例如

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

这篇关于如何在数据源位置使用Sitecore查询?(动态数据源)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆