volume_up

A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

AI OnAI Off

Get latest topics in a forum

Hi,

I would like to display the 5 latest topics from a forum.

Currently my code loks like this:

 var query = new TopicQuery {CreateDate = new DateTimeCriterion()};
            var sortByCreated = new CriterionSortOrder(query.CreateDate, SortingDirection.Descending);
            query.OrderBy.Add(sortByCreated);

            const int pageNumber = 1;
            var topics = ForumHandler.GetQueryResult(query, pageNumber, numberOfTopics);
            return topics;

 

The problem is that this gets topics from all the different forums. How can I get topics from a specific forum?


#46291
Dec 06, 2010 16:54

Any reason why you are using GetQueryResults?

I would create a function like this:

        private static MessageCollection GetRecentlyCreated(RoomBase room, int page, int pageSize, out int totalItems)
        {  
// Recently created TopicSortOrder sortOrder = new TopicSortOrder(TopicSortField.Created, SortingDirection.Descending); MessageCollection topics = ForumHandler.Instance.GetTopics(new RoomCollection { room }, EntityStatus.Approved, page, pageSize, out totalItems, sortOrder); return topics; }
    

Then also add, if you don't have an addition to your pagebase that gets the CurrentRoom you want to fetch from:

        public RoomBase CurrentRoom
        {
            get
            {
                int roomId;
                if (Request.QueryString["roomId"] != null && Int32.TryParse(Request.QueryString["roomId"], out roomId))
                    return ForumHandler.Instance.GetRoom(roomId);

                return ForumHandler.Instance.GetRoom(-1);
            }
        }
    

Then fetch the items and add them to a listview or something similar

int totalItems;
lstView.DataSource = GetRecentlyCreated(CurrentRoom, 1, 5, totalItems);
lstView.DataBind();
  
    

 

#46317
Dec 07, 2010 11:11

Hi,

If you still want to use the QueryResult function, add this to the query:

query.Room = new RoomCriterion();
query.Room.ID = new IntegerCriterion();
query.Room.ID.Includes = new IntegerInCriterion();
query.Room.ID.Includes.Values.AddRange(roomIDs);

where roomIDs is the ids of the rooms in the forum you want get topics from.

// Regards, Torbjörn
 

#47739
Feb 11, 2011 7:43

I am currently working with Relate plus 1. What version do you work in? Anyways, this is how I solved it.

 

TopicQuery topicQuery = new TopicQuery();
topicQuery.CreateDate =
new DateTimeCriterion();
topicQuery.Room =
new RoomCriterion();
topicQuery.Room.Parent =
new RoomCriterion();
topicQuery.Room.Parent.Forum =
new ForumCriterion();
topicQuery.Room.Parent.Forum.ID =
new IntegerCriterion();
topicQuery.Room.Parent.Forum.ID.Value = ForumIDForTheForumYouWantToSearch;

CriterionSortOrder critSort = new CriterionSortOrder(topicQuery.CreateDate, EPiServer.Common.Sorting.SortingDirection.Descending);
topicQuery.OrderBy.Add(critSort);

MessageCollection topics = QueryHandler.GetQueryResult<EPiServer.Community.Forum.Topic, MessageCollection>(topicQuery, 1, ThreadsCount);

Hope this helps :-)


#50894
Edited, May 16, 2011 16:08
error This thread is locked and should be used for reference only. Please use the Legacy add-ons forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.