【问题标题】:Sitecore Content Search, converting to List<item> and editingSitecore 内容搜索,转换为 List<item> 和编辑
【发布时间】: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 对其进行修改?

【问题讨论】:

    标签: c# linq sitecore


    【解决方案1】:

    你可以做的就是将你的结果转换成一个项目的集合:

    var itemList= results.Hits.Select(i => i.Document.GetItem());
    

    这样,您可以像以前一样遍历每个项目并编辑字段值。

    【讨论】:

      【解决方案2】:

      你可以使用下面的代码来做同样的事情 -

      results = context.GetQueryable<SearchResultItem>()
                   .Where(item => item.TemplateId == GlobalId.UniversalContent
                           || item.TemplateId == GlobalId.UniversalHome)
                   .Where(item => item.Path.Contains("/sitecore/content"))
                   .Select(i => (Item)i.GetItem()).ToList();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-02-11
        • 2017-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多