+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / forums - only show the last 30 days of active posts?

forums - only show the last 30 days of active posts?

8 posts, 0 answered
  1. Meister
    Meister avatar
    262 posts
    Registered:
    09 Nov 2007
    03 Feb 2010
    Link to this post
    Hi

    Is it possible to configure the forums to only show the last 30 days worth of posts?

    thanks
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    04 Feb 2010
    Link to this post
    Hi Meister,

    You can show the last 30 post from a thread by subscribing for the ItemDataBound event of allPosts repeater declared in ~/Sitefinity/ControlTemplates/Forums/SingleThread.ascx

    Then you can compare the date and hide the old posts.

    public void Page_Load(object sender, EventArgs e)
       {
     
           allPosts.ItemDataBound += new RepeaterItemEventHandler(allPosts_ItemDataBound);
       }
     
       void allPosts_ItemDataBound(object sender, RepeaterItemEventArgs e)
       {
           if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
           {
               IPost p = e.Item.DataItem as IPost;
               DateTime postTime = p.DateCreated;
               int val = DateTime.Compare(postTime, DateTime.Now.AddDays(-30));
               if (val == -1)
               {
                   // hide the post.
               }
           }
       }


    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
  3. Gawein
    Gawein avatar
    12 posts
    Registered:
    13 Oct 2009
    09 Mar 2010
    Link to this post
    Hey,

    Won't this method go through every single forum post? And if you have hundreds of posts take a while and recources?

    Isn't there a way you can filter it directly in de database?

    Regards,
    Gawein
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    09 Mar 2010
    Link to this post
    Hi Gawein Heymans,

    You can take a look at MSDN - Repeater.ItemDataBound Event to gather more information about this event and when it rises.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  5. Gawein
    Gawein avatar
    12 posts
    Registered:
    13 Oct 2009
    09 Mar 2010
    Link to this post
    Hello Ivan,

    Thank you, I now understand the Repeater.ItemDataBound event.
    I tried your suggested approach but nothing sems to show, what am I doing wrong...

    Regards,
    Gawein

    LatestForumPosts.ascx

    01.<%@ Control Language="C#" AutoEventWireup="true" CodeFile="LatestForumPosts.ascx.cs" Inherits="MyControls_LatestForumPosts" %>
    02.<%@ Register Assembly="Telerik.Cms.Web.UI" Namespace="Telerik.Cms.Web.UI" TagPrefix="sfWeb" %>
    03. 
    04.<asp:Repeater ID="allPosts" runat="server">
    05.    <ItemTemplate>
    06.        <dl>
    07.            <dt><asp:Label ID="anonymousName" runat="server" Text="Anonymous"></asp:Label></dt>
    08.            <dd><asp:Literal ID="authorRole2" runat="server"></asp:Literal></dd>
    09.            <dd>
    10.                (<asp:Literal ID="authorPostCount2" runat="server"></asp:Literal><asp:Literal runat="server" ID="postCountText2" Text="Posts"></asp:Literal>)
    11.            </dd>
    12.        </dl>
    13.        <div>
    14.            <p><sfWeb:DateTimeLiteral runat="server" ID="postDate" DateFormatString="dd MMM yyyy, hh:mm" /></p>
    15.            <p><asp:Literal ID="postContent" runat="server"></asp:Literal></p>
    16.        </div>       
    17.        <p>
    18.            <asp:LinkButton ID="btnEdit" runat="server" CommandName="EditPost" Text="Bewerken"></asp:LinkButton>
    19.            <asp:LinkButton ID="btnDelete" runat="server" CommandName="DeletePost" Text="Verwijderen"></asp:LinkButton>
    20.            <asp:LinkButton ID="btnReply" runat="server" CommandName="ReplyToPost" Text="Antwoorden"></asp:LinkButton>
    21.        </p>
    22.    </ItemTemplate>
    23.</asp:Repeater>

    LatestForumPosts.ascx.cs

    01.using System;
    02.using System.Collections.Generic;
    03.using System.Linq;
    04.using System.Web;
    05.using System.Web.UI;
    06.using System.Web.UI.WebControls;
    07.using Telerik.Forums;
    08. 
    09.public partial class MyControls_LatestForumPosts : System.Web.UI.UserControl
    10.{
    11.    protected void Page_Load(object sender, EventArgs e)
    12.    {
    13.        allPosts.ItemDataBound += new RepeaterItemEventHandler(allPosts_ItemDataBound);
    14.    }
    15. 
    16.    void allPosts_ItemDataBound(object sender, RepeaterItemEventArgs e)
    17.    {
    18.        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    19.        {
    20.            IPost p = e.Item.DataItem as IPost;
    21.            DateTime postTime = p.DateCreated;
    22.            int val = DateTime.Compare(postTime, DateTime.Now.AddDays(-30));
    23.            if (val == -1)
    24.            {
    25.                // hide the post.
    26.            }
    27.        }
    28.    }
    29.}
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    09 Mar 2010
    Link to this post
    Hello Gawein Heymans,

    You can attach the code to VS debugger and see which posts are added to the repeater. It is possible that the posts you have does not math the DateTime condition. You can add some div to the template that will hide the posts.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  7. Gawein
    Gawein avatar
    12 posts
    Registered:
    13 Oct 2009
    09 Mar 2010
    Link to this post
    It doesn't hide anything, shouldn't it just show all?
  8. Mark
    Mark avatar
    86 posts
    Registered:
    08 Feb 2010
    08 Aug 2010
    Link to this post
    Gawein, did you get this working?
    I'd would like to do something similar, and would like to know how you mananged it?
Register for webinar
8 posts, 0 answered