【发布时间】:2017-06-22 21:21:45
【问题描述】:
我正在使用 Sitecore 7 第一次深入研究 ContentSearchAPI,但遇到了一些麻烦。对于一个假设的问题,我决定编写一个搜索,为我的网站索引整个树,提取两个模板之一并位于特定路径中的项目。查询代码为:
using (var context = index.CreateSearchContext())
{
var results = context.GetQueryable<SearchResultItem>()
.Where(item => item.TemplateId == GlobalId.UniversalContent
|| item.TemplateId == GlobalId.UniversalHome)
.Where(item => item.Path.Contains("/sitecore/content"))
.GetResults();
}
到目前为止一切顺利;搜索抓取所需的项目。但现在我想在编辑模式下打开项目并更改所述项目上字段的值。以前,当我使用 Sitecore 查询时,我使用类似以下的方法来完成此操作:
foreach (Item i in itemList)
{
using (new SecurityDisabler())
{
i.Editing.BeginEdit();
i.Fields["DoNotIndex"].SetValue("1", true);
i.Editing.EndEdit();
}
}
但是LINQ查询的返回类型是SearchResultItem,因此它不继承Item方法,包括Editing.BeginEdit()等。我玩弄了 LINQ 查询并将 Cast() 或 OfType() 与 ToList() 结合使用,但不断收到错误消息,指出我无法将 SearchResultItem 转换为 Item。
阅读 Sitecore POCO 似乎让我朝着正确的方向前进,但这似乎更多地是关于定义要阅读的字段。有没有办法将 SearchResultItem 转换为 Item 并打开该项目进行编辑?使用 LINQ 和 ContentSearch API,我是否需要创建 POCO,定义我想要的字段,然后使用 POCO 对其进行修改?
【问题讨论】: