CommunityServer: Custom Homepage With Thread Listing

In this Community Server tip, I'll describe how to make a quick & easy home page that displays posts from one or more forums on one page.. If you're concerned that new visitors to your community will be turned off by the default listing of forums when they go to www.YourSite.com, then this just may be for you. I think you'll find that most of the time you spend on this tip will be in the actual design of the home page. This is what I do for TheDailyWtf.com.

Step One - Layout

The standard default.aspx that comes with Community Server contains the title banner, site navigation tabs, and an area with some explanatory copy ("Community Server is a rich knowledge management and collaboration platform designed ..."). Keep as much or as little as you want; I opted to replace the main area with a two column view: a left "side-bar" and a center posts. Before you start coding, you may find it easier to put in dummy place holder post text where your normal posts would appear.

Step Two - Default.aspx Header

At the top of the top of the page, you'll want to make sure that you have the appropriate references to the Community Server components. Here is what I have at the top of my file. Some of these may or may not be already in the existing default.aspx:

<%@ Page SmartNavigation="False" Language="VB" %><%@ Register TagPrefix="CS" Namespace="CommunityServer.Controls" Assembly="CommunityServer.Controls" %><%@ Import Namespace="CommunityServer.Galleries.Components" %><%@ Import Namespace="CommunityServer.Blogs.Components" %><%@ Import Namespace="CommunityServer.Components" %><%@ Import Namespace="CommunityServer" %><%@ Import Namespace="CommunityServer.Discussions.Components" %>

<%@ Page SmartNavigation="False" Language="VB" %> <%@ Register TagPrefix="CS" Namespace="CommunityServer.Controls" Assembly="CommunityServer.Controls" %> <%@ Import Namespace="CommunityServer.Galleries.Components" %> <%@ Import Namespace="CommunityServer.Blogs.Components" %> <%@ Import Namespace="CommunityServer.Components" %> <%@ Import Namespace="CommunityServer" %> <%@ Import Namespace="CommunityServer.Discussions.Components" %> 

Note that I have Language="VB" at the top. Although Community Server is entirely C#, I prefer to code in VB and will whenever I get a chance. Ahh, the power of .NET.

Step Three - Add The Code

Just place this block of code and place it under your page header Import statements:

<script runat="server">
dim sideBar as ThreadSet
dim main as ThreadSet
sub Page_Load
  sideBar = Threads.GetThreads( _
  18,0,5, Users.GetAnonymousUser(), _
  DateTime.MinValue,SortThreadsBy.PostDate,SortOrder.Descending, _
  ThreadStatus.Open, ThreadUsersFilter.All,false,false,false,false)
  
 main = Threads.GetThreads( _
  12,0,5, Users.GetAnonymousUser(), _
  DateTime.MinValue,SortThreadsBy.PostDate,SortOrder.Descending, _
  ThreadStatus.Open, ThreadUsersFilter.All,false,false,false,false)
    
  DataBind
end sub </script>

The code is very basic. It declares two ThreadSets (a group of threads within a forum), fills them when the page loads, and then binds the thread sets to the repeater (which we will build in Step Four).

Since the GetThreads() arguments are a bit intimidating, I'll explain them one-by-one, in order:

  • forumID - The ID of the forum to get the threads for. For my site, 12 is the ID of the main forum and 18 is of the sidebar.You will definitely need to change this to the forum you'd like to display.
  • pageIndex - Because a forum can contain more than one "page" of threads, you need to specify which page to retrieve. We're getting the threads from the first page, which is 0.
  • pageSize - The number of threads to display per page. Both of my thread sets show five per page.
  • user - The user who is requesting the threads. If you wanted, you could use the CurrentUser. I chose to display the threads as an anonymous user would have seen. I honestly think it makes little difference.
  • threadsNewerThan - I want to show 5 threads, regardless of age. This is why I use DateTime.Min -- nothing can be older than that value. If you want to show the latest 10 days of threads, you could use Now.AddDays(-10) instead.
  • sortBy - This refers to that drop down box in the sort options. Note that I'm using the custom one I discussed last time.
  • sortOrder - This can be Ascending (old to new) or Descending (new to old).
  • threadStatus - This can be Open, Closed, Resolved. You will most likely want Open
  • userFilter - This can be All, HideTopicsParticipatedIn, HideTopicsNotParticipatedIn,        HideTopicsByAnonymousUsers, or HideTopicsByNonAnonymousUsers. I think they're all pretty descriptive.
  • activeTopics - true or false, indicates whether to show only active topics. An Active Topic is defined as (I believe) one where there was activity within the past seven days. I chose false.
  • unreadOnly - true of false, indicates whether to only show unread. I think false is the best choice here.
  • unansweredOnly - true of false, indicates whether to only show unanswered. I think false is the best choice here as well.
  • returnRecordCount - true or false, indicates whether to return a record count or not. I said false because I'm not using a record count on the page

That wasn't so bad, now was it?

Step Four - Add the Repeater(s)

Now that we have the threadsets coded, we need to add some repeaters to display them. These are a very basic control to use. Here is a very basic code. As you can probably tell, this will have no styling whatsoever. This is all for you to add :-).

<asp:repeater runat=server datasource=<%#main.Threads%>> <itemtemplate> <h2><%#Container.DataItem.Subject%></h2> <div> <em><%#Container.DataItem.PostDate.ToLongDateString%></em>
<br/> <div><%#Container.DataItem.Body%></div> </div> </itemtemplate> </asp:repeater>

Note the datasource. Simply change that to whichever threadset (declared in step two) you'd like to display. Just play around with the formatting, and you're good to go. If you'd like, feel free to look at the source code of my default.aspx. Mostly you'll be able to see how I have links back to the post and some other things. It's pretty basic; you'll probably be able to figure it out by poking around. 

3 Comments

Comments have been disabled for this content.