【问题标题】:Sitecore get list of content items using a recently updated media itemSitecore 使用最近更新的媒体项获取内容项列表
【发布时间】:2016-03-31 21:08:20
【问题描述】:

这是场景: 我在 Sitecore 7 (Update-4) 中有一个页面,其中显示了最近更新的内容和媒体的列表。对于媒体项目,我需要显示使用最近更新的媒体项目的内容项目路径列表。这样做的最佳方法是什么?

我下面的代码使用 Sitecore 快速查询,因为最初这应该是一个简单的页面,列出每个项目的几个字段。我们还没有完全实现我们的搜索索引(或者如果你喜欢的话),但是如果我需要添加一个带有计算字段的自定义搜索索引来完成这个,那就这样吧!我们还没有实现 ORM(因此需要通过 id 获取持续时间设置项)。另外请记住,我从不了解 Sitecore 的其他人那里继承了这个项目,因此我不得不使用“虚拟胶带”在某些地方完成工作。

<div id="content">
  <h1>Recently Updated Content</h1>
  <asp:Repeater id="rptRecentlyUpdatedContent" runat="server">
    <HeaderTemplate>
      <Table class="gridtable">
        <tr>
           <th>Name</th>
           <th>Section</th>
           <th>Type</th>
           <th>Last Updated</th>
        </tr>
    </HeaderTemplate>

    <ItemTemplate>
      <tr>
        <td><%# DataBinder.Eval(Container.DataItem, "Name") %></td>
        <td><%# DataBinder.Eval(Container.DataItem, "Paths.ContentPath") %></td>
        <td>Content</td>
        <td><%# Sitecore.DateUtil.IsoDateToDateTime(DataBinder.Eval(Container.DataItem, "Fields[\"__Updated\"]").ToString()) %></td>
      </tr>
    </ItemTemplate>
  </asp:Repeater>

  <asp:Repeater id="rptRecentlyUpdatedFiles" runat="server">
    <ItemTemplate>
      <tr>
        <td><%# DataBinder.Eval(Container.DataItem, "Name") %></td>
        <td></td>
        <td><%# DataBinder.Eval(Container.DataItem, "TemplateName") %></td>
        <td><%# Sitecore.DateUtil.IsoDateToDateTime(DataBinder.Eval(Container.DataItem, "Fields[\"__Updated\"]").ToString()) %></td>
      </tr>
    </ItemTemplate>

    <FooterTemplate>
      </Table>
    </FooterTemplate>
  </asp:Repeater>
</div>

代码隐藏:

using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RJ.Sitecore.Web.layouts.controls
{
    public partial class RecentUpdates : BaseControl
    {     

        protected void Page_Load(object sender, EventArgs e)
        {
            int duration = GetRecentlyUpdatedDuration();
            GetRecentlyUpdatedItems(duration);
        }

        protected void GetRecentlyUpdatedItems(int duration) 
        {
            try
            {
                DateTime dtEndDate = DateTime.Now.AddDays(1);
                DateTime dtStartDate = dtEndDate.AddDays(-duration);
                string endDate = dtEndDate.ToString("yyyyMMdd");
                string startDate = dtStartDate.ToString("yyyyMMdd");
                string sitecoreContentQuery = string.Format("fast:/sitecore/content/Home//*[@__Updated >= '{0}T000000' and @__Updated < '{1}T000000']", startDate, endDate);
                string sitecoreMediaQuery = string.Format("fast:/sitecore/media library//*[@__Updated >= '{0}T000000' and @__Updated < '{1}T000000']", startDate, endDate);

                global::Sitecore.Data.Items.Item[] contentItems = global::Sitecore.Context.Database.SelectItems(sitecoreContentQuery);
                var orderedContentItems = contentItems.OrderByDescending(x => x[global::Sitecore.FieldIDs.Updated]);

                global::Sitecore.Data.Items.Item[] mediaItems = global::Sitecore.Context.Database.SelectItems(sitecoreMediaQuery);
                var orderedMediaItems = mediaItems.OrderByDescending(x => x[global::Sitecore.FieldIDs.Updated]);

                rptRecentlyUpdatedContent.DataSource = orderedContentItems;
                rptRecentlyUpdatedContent.DataBind();
                rptRecentlyUpdatedFiles.DataSource = orderedMediaItems;
                rptRecentlyUpdatedFiles.DataBind();
            }
            catch (Exception ex)
            {
                global::Sitecore.Diagnostics.Log.Error("ERROR: ", ex); 
            }
        }

        protected int GetRecentlyUpdatedDuration()
        {
            Item durationItem = global::Sitecore.Context.Database.GetItem("{05A9B5E8-C9D3-4532-B3E1-301546BB17BC}");
            if (durationItem == null)
            {
                return 0;
            }

            global::Sitecore.Data.Fields.TextField duration = durationItem.Fields["duration"];
            if (duration == null)
            {
                return 0;
            }

            return Convert.ToInt32(duration.Value);        
        }
    }
}

【问题讨论】:

  • 您是否正在使用或考虑使用Sitecore Powershell Extensions 模块?那里有一个报告将获得最近更新的媒体,将其扩展到内容也不难。

标签: sitecore sitecore7


【解决方案1】:

这是一个很大的话题,所以“最好的方法是什么?”并没有一个简单的答案。所以我希望一个简短而实用的答案就足够了:)

我认为您不需要计算域。用于搜索的基本SearchResultItem 类具有表示上次更新日期和项目路径的属性。所以一个非常基本的搜索可能看起来像这样:

using (var context = ContentSearchManager.GetIndex("your_index_name").CreateSearchContext()) 
{ 
    var results = context.GetQueryable<SearchResultItem>().
                          Where(item => item.Updated > startDate && 
                                        item.Updated < endDate && 
                                        item.Path.StartsWith(mediaLibraryRootPath)).
                          .GetResults();
}

有不同的方法来处理结果,但为了简单起见,我将向您展示如何获取您可以使用的项目 ID 列表

results.Hits.Select(hit => hit.Document.ItemId);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多