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

Forums / Developing with Sitefinity / Get thread name of last post

Get thread name of last post

8 posts, 0 answered
  1. Jeff
    Jeff avatar
    118 posts
    Registered:
    08 Jul 2011
    25 Oct 2012
    Link to this post
    I'm using the forums module in SF 5.1 and I'm trying to retrieve the thread name of the last post? Looking at the "List of Forums" template I can retrieve the user name of the last post and the url to it, etc. but I dont see a way to get the thread name from which that post came from.

    Can someone help?

    Jeff
  2. Patrick Dunn
    Patrick Dunn avatar
    237 posts
    Registered:
    03 Nov 2014
    29 Oct 2012
    Link to this post
    Hi Jeff,

    Thanks for using Sitefinity.

    If you are trying to get the "title" of the forum post you can query for it using the following in your template:

    <sitefinity:TextField runat="server" DisplayMode="Read" Value='<%# Eval("Title")%>' />

    So, effectively if you were trying to provide a link where users click the forum title to go to the most recent post you could do the following:

    <a href='<%# Eval("ItemDefaultUrl")%>'> <%# Eval("Title")%> </a>

    Let me know if this clears things up for you. If you have any questions feel free to ask away!

     

    All the best,
    Patrick Dunn
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Jeff
    Jeff avatar
    118 posts
    Registered:
    08 Jul 2011
    30 Oct 2012 in reply to Patrick Dunn
    Link to this post
    This retrieves the name of the forum, not the thread. See below. I'm trying to get the thread name "Electronics".

    Here's what I would like my forum list to look like:

    - General Discussions (forum group)
         - Classifieds (forum)             5 threads / 9 Posts              Lastest Post: 2 hrs ago in Electronics By: Jeff
  4. Stephen2
    Stephen2 avatar
    94 posts
    Registered:
    05 Feb 2012
    01 Nov 2012
    Link to this post
    I think this will work:

    var latestPost = ForumsManager.GetManager().GetPosts().Where(p => p.IsPublished && !p.IsMarkedSpam).OrderByDescending(p => p.DateCreated).FirstOrDefault();
     
    if (latestPost != null) {
         var latestThreadTitle = latestPost.Thread.Title;
    }

  5. Jeff
    Jeff avatar
    118 posts
    Registered:
    08 Jul 2011
    01 Nov 2012 in reply to Stephen2
    Link to this post
    Thanks Stephen but I'm working in the "List of Forums" widget template and that won't work without code behind and a lot of other logic to accomodate the RadListView. I'm trying to keep it simple if possible before going that route.
  6. Patrick Dunn
    Patrick Dunn avatar
    237 posts
    Registered:
    03 Nov 2014
    02 Nov 2012
    Link to this post
    Hi,

     Stephen's solution should work just fine. You can put it in the code behind for your widget and do something like this:

    public string ForumTitle()
    {
    var latestPost = ForumsManager.GetManager().GetPosts().Where(p => p.IsPublished && !p.IsMarkedSpam).OrderByDescending(p => p.DateCreated).FirstOrDefault();
      
    if (latestPost != null) {
         var latestThreadTitle = latestPost.Thread.Title;
    }
    }
     
    <%# ForumTitle() %>

    If you decide on going with code-behind for the widget template, or using an external widget template I believe you might find this blog post on the topic useful.

    Regards,
    Patrick Dunn
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Stephen2
    Stephen2 avatar
    94 posts
    Registered:
    05 Feb 2012
    02 Nov 2012 in reply to Patrick Dunn
    Link to this post
    Sorry Jeff, I've played around and can't see any way around adding code-behind.

    The problem is the RadListView is bound with a collection of ForumListDataItem, which is an internal class of Telerik.Sitefinity.Forums.dll, therefore there's not much access you can get.  Even if you could, I can't see any easy way of converting back to Forum, to access the threads... And even if you could, you're EVAL line is suddenly very, very long and complicated.

    Rather than the method Patrick Dunn suggested to add code behind, I am going to point you at an (in my opinion) simpler and cleaner method:
    Howto Guide: Add Code behind to a widget template

    And now I will detail the exact way to get your desired result (NOTE: this way a very complex one):

    1) Edit your Widget Template to add the Code Behind (feel free to alter namespaces)
    <%@ Control Language="C#" Inherits="SitefinityWebApp.Websilk.ForumsList" %>

    2) Also add a LABEL (or other control you wish to output the Thread title into).  NOTE: For this example, it's placement inside the <Else> tag of the <sf:Conditional> is vital.
    <Else>
        <asp:Label id="threadTitle" runat="server" />
        <td class="sfforumTitleWrp">

    3) Save and close your widget template

    4) In Visual Studio, add a new "Class" file called "ForumsList.cs" anywhere, paste this exact code in:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using Telerik.Web.UI;
    using Telerik.Sitefinity.Forums.Model;
    using Telerik.Sitefinity.Model;
    using Telerik.Sitefinity.Forums;
    using System.Web.UI.WebControls;
    using Telerik.Sitefinity.Web.UI;
     
    namespace SitefinityWebApp.Websilk {
        public class ForumsList : UserControl {
            protected void Page_Load(object sender, EventArgs e) {
                var list = (RadListView)this.FindControl("list");
                list.ItemDataBound += new EventHandler<RadListViewItemEventArgs>(list_ItemDataBound);
            }
     
            void list_ItemDataBound(object sender, RadListViewItemEventArgs e) {
                if (e.Item.ItemType == RadListViewItemType.DataItem || e.Item.ItemType == RadListViewItemType.AlternatingItem) {
                    //Get the bound item
                    var args = (RadListViewDataItem)e.Item;
     
                    //The only way to access details of an internal class is with reflection
                    var prop = args.DataItem.GetType().GetProperty("DataItem");
                    var forumItem = prop.GetValue(args.DataItem, null);
     
                    //This control binds both ForumGroup and Forum
                    if (forumItem is Forum) {
                        //Yay, got the forum, get the last thread
                        var forum = (Forum)forumItem;
                        var lastThreadTitle = forum.GetThreads().Single(t => t.Id == forum.LastThreadId).Title;
     
                        //The label is stored in the conditional template... find it!
                        var conditional = (Conditional)e.Item.FindControl("conditional");
                        var threadTitle = (Label)conditional.Controls[0].FindControl("threadTitle");
     
                        //Display the title!                   
                        threadTitle.Text = lastThreadTitle;
                    }
                }
            }
        }
    }

    Compile and view your page, it should work.  Let me know if you have any troubles, and I hope this shows you the power of adding code behind to your widget templates for these very complex scenarios.

    NOTE: If the internal class ForumListDataItem was extended to include this by default, a simple EVAL("LastThreadTitle") would have worked...  Might be a nice feature request.
  8. Jeff
    Jeff avatar
    118 posts
    Registered:
    08 Jul 2011
    02 Nov 2012 in reply to Stephen2
    Link to this post
    Hey Stephen,

    I read you're post about adding code behind to widget templates a few months ago and have used it quite extensively so I am quite familiar with it. I was trying to avoid this approach as it seemed like overkill just to get the thread name of the last post. As you stated, had they simply extended the ForumListDataItem to include it this would have been very simple.

    Any way, thanks for the code! It saves me the time of writing it myself.
8 posts, 0 answered