以下是场景:我在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);
}
}
}发布于 2016-04-01 06:44:35
这是一个很大的话题,所以“做这件事的最佳方法是什么?”并没有一个简单的答案。因此,我希望一个简短而实用的答案就足够了:)
我认为您不需要计算字段。用于搜索的基类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);https://stackoverflow.com/questions/36343806
复制相似问题