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

标签 sitecore sitecore6

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

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

子布局数据源位置应指向当前项目下的文件夹。所以我尝试将数据源位置设置为 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 - 如何在数据源位置使用 sitecore 查询? (动态数据源),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7678597/

相关文章:

sitecore - 使用个人资料卡以编程方式跟踪对项目的访问

sitecore - 为 Sitecore 设置开发环境的最佳方法是什么

sitecore - 设置 __Renderings 字段未在 Sitecore 结果中共享?

asp.net - Sitecore 用于创建项目的默认语言

sitecore - 从另一个分支获取 Sitecore sibling

sitecore - 跟踪 Sitecore 项目历史记录

sitecore - Sitecore MaxMind选项

css - 将多个 css 类添加到 WFFM 字段

asp.net - Glass Mapper 打破了图像场的标准值

caching - Sitecore 8 : leverage browser caching