templates - 将 Sitecore 模板字段源值设置为动态 Sitecore 查询

标签 templates dynamic field sitecore

我有一个用例,其中用户需要从项目下拉列表中选择字段值。这样做的问题是,需要在每个项目(均使用相同的模板)上动态构建下拉列表,以仅显示文件夹中字段值等于当前项目 ID 的项目。如果您已经迷路了,这里是一个结构示例:

- sitecore
  - content
    - Home
      - ContentItem1 (with droplist)
    - Site Data
      - SelectableItem1(ContentItem1 selected in 'itemid' field)
      - SelectableItem2(ContentItem1 selected in 'itemid' field)
      - SelectableItem3(ContentItem1 not selected in 'itemid' field)
      - SelectableItem4(ContentItem1 not selected in 'itemid' field)
  - templates
    - ContentItem1Template
      - Droplist field (source set to below query)

我希望我的查询通过获取以 ContentItem1 的 id 作为“itemid”字段值的项目列表来动态分配 ContentItem1 的下拉列表字段源,但通过将该字段值与 ContentItem1 id 的字段值进行比较。我尝试通过将字段的值与 id token 进行比较来做到这一点,如下所示:

query:/sitecore/content/Site Data/*[@#itemid#=$id]

无论我尝试为 id 设置什么值('$id'、$id、@id、'@id'、@@id、'@@id' 等),它都不想在该项目上解析等级。有什么方法可以做到这一点,以便我可以为需要相同功能的所有项目重用此 ContentItem1Template?

最佳答案

如果您使用的是 Sitecore 7,则可以使用编码字段数据源。这将允许您使用任何您喜欢的自定义逻辑来指定应出现在列表中的项目。

创建一个实现 IDataSourceListQuery() 方法的类,该方法返回项目列表作为字段的源。然后将字段的源设置为带有 code: 前缀的方法,例如代码:MyProject.Custom.FieldDataSource,MyProject.Custom

using System;
using Sitecore.Buckets.FieldTypes;
using Sitecore.Data.Items;

namespace MyProject.Custom
{
  public class FieldDataSource : IDataSource
  {
    public Item[] ListQuery(Item item)
    {
      var root = item.Database.GetItem("/sitecore/content/my-item");
      // some other logic to filter your item set
      return root.Children.ToArray();
    }
  }
}

这些文章应该对您有帮助:

关于templates - 将 Sitecore 模板字段源值设置为动态 Sitecore 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22922541/

相关文章:

c# - 有什么区别...? [C# 属性 GET/SET 方式]

actionscript-3 - 使用属性而不是公共(public)变量有什么优势吗?

c++ - 原子读/写值的静态断言

c++ - C++中的模板工厂模式

c# - .NET 中的动态控件问题

spring - 动态添加实体到 hibernate

linq-to-sql - 为什么 LinqPad 创建字段而不是属性?

c++ - 在类内部和外部定义的模板类的成员函数之间的区别

c++ - 模板的自动返回类型和歧义

php - 将变量设置为 sql 查询的字段名称