【问题标题】:How to use sitecore query in datasource location? (dynamic datasource)如何在数据源位置使用 sitecore 查询? (动态数据源)
【发布时间】:2011-10-06 18:07:59
【问题描述】:

是否可以将数据源位置(不是数据源)设置为站点核心查询?

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

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

【问题讨论】:

    标签: sitecore sitecore6


    【解决方案1】:

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

    ./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,虽然这是在页面编辑器使用的子布局的数据源位置上。选择项目后,它仍然是绝对路径(我们在 item:saving 处理程序中将其更改为 GUID)。如果您想在一般数据源中支持相对路径,您可以自定义 SublayoutParamHelper 实用程序类(或您的版本)以执行 Sitecore 查询而不是 GetItem。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    相关资源
    最近更新 更多