0

日付ごとに上位 2 つの最新アイテムを表示しようとしているサイトコア Web パーツがあります。最新のアイテムしか表示できなくて困っています。

ascx コードの場合:

    <asp:Repeater ID="newsContainer" runat="server">

        <ItemTemplate>
    <div class="newsItem">
    <h2><a href="/about-us/news.aspx">News</a></h2>
        <a href="<%# SitecoreUtility.NavigateUrl((Item)Container.DataItem)%>" class="newsHeadline">

 <span>  <sc:Date ID="Date1" Item="<%# Container.DataItem %>" Field="Posted-Date" Format="MM.dd.yyyy" runat="server" />    
 <sc:Text ID="Text1" Item="<%# Container.DataItem %>" Field="Title" runat="server" />
    </span>

            </a>

         <!--  <p class="newsSummary">
                <asp:Literal ID="litBody" runat="server" Text="<%# SitecoreUtility.TruncateByWords(((Item)Container.DataItem).Fields[Constants.Fields.BODY].Value, 20) %>"></asp:Literal>
                 ...
                <a href="<%# SitecoreUtility.NavigateUrl((Item)Container.DataItem)%>"     class="more">+ More</a></p> -->
        </div>
    </ItemTemplate>
</asp:Repeater>


<asp:Panel ID="pnlPagination" runat="server"></asp:Panel>
<asp:Panel ID="pnlArchive" runat="server">
  <!--  <div class="newsArchiveLink">
      <a href="?type=archive">View Archived News</a>
    </div> -->
</asp:Panel>
<asp:Panel ID="pnlCurrent" runat="server">
    <div class="newsArchiveLink">
      <a href="?type=current">View Current News</a>
    </div>
</asp:Panel>

ASCX.CS ファイルの場合:

public partial class Homepage_NewsListing : BaseSublayout
{
    int CurrentPage = 1;
    int PageSize = Constants.Values.SEARCH_SMALL_LIST;

    protected void Page_Load(object sender, EventArgs e)
    {


        CurrentPage = int.Parse(WebUtil.GetQueryString("page", "1"));

        List<Item> newsListings = new List<Item>();
        Item newsItems = SitecoreUtility.Db.GetItem(Constants.Items.NEWS);

        if (WebUtil.GetQueryString("type", "") == "archive")
        {
            newsListings = newsItems.Children.Where(item =>
                        (SitecoreUtility.FormatDateAtMidnight(item, Constants.Fields.ARCHIVEDATE) <= DateTime.Now)
                        &&
                        (!string.IsNullOrEmpty(item.Fields[Constants.Fields.ARCHIVEDATE].Value))
                    ).OrderBy(item => item.Fields[Constants.Fields.POSTED_DATE].Value).ToList();

            pnlArchive.Visible = false;
            //pnlCurrent.Visible = true;
            pnlCurrent.Visible = false;
        }
        else
        {
            newsListings = newsItems.Children.Where(item =>
                        (SitecoreUtility.FormatDateAtMidnight(item, Constants.Fields.ARCHIVEDATE) > DateTime.Now)
                        ||
                        (string.IsNullOrEmpty(item.Fields[Constants.Fields.ARCHIVEDATE].Value))
                    ).OrderBy(item => item.Fields[Constants.Fields.POSTED_DATE].Value).ToList();

            //pnlArchive.Visible = true;
            pnlArchive.Visible = false;
            pnlCurrent.Visible = false;
        }
        newsContainer.DataSource = DisplayResults(newsListings.OrderByDescending(item => item.Fields[Constants.Fields.POSTED_DATE].Value).ToList());
        newsContainer.DataBind();
    }

    protected List<Item> DisplayResults(List<Item> results)
    {
        pnlPagination.Controls.Add(new Pager(results.Count, PageSize, CurrentPage).BuildPaging());

        if (CurrentPage > 1)
            return results.Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList();

        return results.Take(PageSize).ToList();
    }
}
4

1 に答える 1

1

最初に、コードをもう少し単純にして (デバッグ用に)、高速なクエリを使用して日付範囲内のアイテムを取得するようにします。

var date = DateTime.Now.AddMonths(3);
var dateString = date.Year.ToString() + date.Month.ToString().PadLeft(2, '0') +     date.Day.ToString().PadLeft(2, '0');
var newsList = Sitecore.Context.Database.SelectItems("fast:/sitecore/content/home/your-path-to-news/*[@@templatename='Your template name' and @POSTED_DATE > '" + dateString + "']").ToList();
newsList = newsList.OrderByDescending(n => n.Fields["POSTED_DATE"].Value).Take(2).ToList();

newsContainer.DataSource = newsList;
newsContainer.DataBind();
于 2013-02-23T06:05:27.570 に答える