Sitecore使用最近更新的媒体项获取内容项列表

时间:2016-03-31 21:08:20

标签: sitecore sitecore7

以下是该方案: 我在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);        
        }
    }
}

1 个答案:

答案 0 :(得分: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);
相关问题