我有一个包含 DropList 字段的数据模板。我希望数据源来自两个 Sitecore 文件夹项目。
有没有办法为下拉列表定义多个来源?
最佳答案
不在标准下拉列表字段中,但基于从 source 字段获取 2 个参数的 Droplist 创建自定义 sitecore 字段应该不会太难。
这是创建自定义控件的好资源:http://sitecorejunkie.com/2012/12/28/have-a-field-day-with-custom-sitecore-fields/
下拉列表控件使用 Sitecore.Shell.Applications.ContentEditor.ValueLookupEx 作为其控件。因此,您可以创建一个继承自该控件的新控件,并覆盖 GetItems() 方法以从您的源中读取项目
当前的看起来像这样:
protected override Item[] GetItems(Item current)
{
Assert.ArgumentNotNull((object) current, "current");
using (new LanguageSwitcher(this.ItemLanguage))
return LookupSources.GetItems(current, this.Source);
}
所以你可以让源有 2 个 guids/路径,用竖线 (|) 分割
protected virtual Item[] GetItems(Item current)
{
Assert.ArgumentNotNull((object) current, "current");
using (new LanguageSwitcher(this.ItemLanguage))
{
var sourceList = this.Source.Split('|');
var items = LookupSources.GetItems(current, source[0]).ToList();
items.AddRange(LookupSources.GetItems(current, source[1]));
return items.ToArray();
}
}
- 免责声明 - 此代码未经测试,但应该为您指明正确的方向。
关于c# - 具有多个来源的 Sitecore 下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22618705/