Get thread name of last 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
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")%>' /><a href='<%# Eval("ItemDefaultUrl")%>'> <%# Eval("Title")%> </a>All the best,
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
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;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.
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,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" %><Else> <asp:Label id="threadTitle" runat="server" /> <td class="sfforumTitleWrp">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; 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.