首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在数据源位置使用sitecore查询?(动态数据源)

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

Stack Overflow用户
提问于 2011-10-07 02:08:00
回答 1查看 5.3K关注 0票数 5

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

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-10-07 07:27:36

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

代码语言:javascript
复制
./Items

显然,该文件夹需要已经存在。我一直想把这段代码写在博客上,这可能有点夸张,但我会在这里发表,因为它可能会对你有所帮助。可以将以下内容添加到getRenderingDatasource管道中,以创建一个相对路径数据源位置(如果它还不存在)。将其添加到GetDatasourceLocation处理器之前。

在子布局中,您需要添加一个参数contentFolderTemplate=[GUID]来指定所创建项目的模板。

代码语言:javascript
复制
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)));
                    }
                }
            }
        }
    }
}
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7678597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档