我有一个用例,其中用户需要从项目下拉列表中选择字段值。这样做的问题是,需要在每个项目(均使用相同的模板)上动态构建下拉列表,以仅显示文件夹中字段值等于当前项目 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,则可以使用编码字段数据源。这将允许您使用任何您喜欢的自定义逻辑来指定应出现在列表中的项目。
创建一个实现 IDataSource
和 ListQuery()
方法的类,该方法返回项目列表作为字段的源。然后将字段的源设置为带有 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/