NullReferenceException When Getting Latest Topics and Replies For Forums in User's Clubs

StackOverflow https://stackoverflow.com/questions/4014775

  •  26-09-2019
  •  | 
  •  

Question

I'm trying to query the set of forums (fora?) for the user's clubs to get the latest topics and replies in those forums. This is the code I'm using:

RoomCollectionCriterion userClubRoomsCollection = new RoomCollectionCriterion();

foreach (Club userClub in userClubCollection)
{
    RoomCriterion userClubRoomCriterion = new RoomCriterion();
    userClubRoomCriterion.ID = new IntegerCriterion();
    userClubRoomCriterion.ID.Value = userClub.ForumRoom.ID;

    userClubRoomsCollection.Criteria.Add(userClub.Name, userClubRoomCriterion);
}

TopicQuery topicQuery = new TopicQuery();

topicQuery.Room = new RoomCriterion();
// Pass in our collection of the user's clubs to the query
topicQuery.Room.Rooms = userClubRoomsCollection;

MessageCollection topics = QueryHandler.GetQueryResult<EPiServer.Community.Forum.Topic, MessageCollection>(topicQuery, pageNumber, 2);
MessageCollection replies = QueryHandler.GetQueryResult<EPiServer.Community.Forum.Topic, MessageCollection>(topicQuery, pageNumber, 4);

The problem I have is that every time I hit the first call to GetQueryResult, I get a NullReferenceException. I can't see anything wrong/missing with what I'm passing in to GetQueryResult, but I'm hoping somebody else has done this previously and can suggest a fix for this.

Update 26/10/2010
As suggested by Magnus I swapped out my QueryHandler.GetQueryResult for a call to ForumHandler.GetQueryResult but this doesn't seem to have made a difference. I've also trimmed down the parameters to GetQueryResult so that my line of code is now:

MessageCollection topics = ForumHandler.GetQueryResult(topicQuery);

but this still gives me a NullReferenceException. Which suggests to me the problem isn't in the GetQueryResult method of whichever handler I'm calling, it's in the topicQuery object, as that's the thing that's stayed constant so far. I'm wondering if I'm missing a property that needs to be set on the TopicQuery object.

Update 4/11/2010
Is there a "security context" that needs consideration here? i.e. the query needs to be run in the context of a user that has complete access to all forums?

Was it helpful?

Solution 2

The solution to this (as found by my colleague) is:

TopicQuery topicQuery = new TopicQuery();

//For latest topics we want to sort by the topics creation date.
topicQuery.CreateDate = new DateTimeCriterion();
topicQuery.Room = new RoomCriterion();
topicQuery.Room.ID = new IntegerCriterion();
//We want to include several roomIDs
topicQuery.Room.ID.Includes = new IntegerInCriterion();

//For each club, get its ForumRoom
foreach (Club userClub in userClubCollection)
{
    topicQuery.Room.ID.Includes.Values.Add(userClub.ForumRoom.ID);
}

//Sorting of results
CriterionSortOrder critSort = new CriterionSortOrder(topicQuery.CreateDate, EPiServer.Common.Sorting.SortingDirection.Descending);

//Add sorting to query
topicQuery.OrderBy.Add(critSort);

MessageCollection topics = QueryHandler.GetQueryResult<EPiServer.Community.Forum.Topic, MessageCollection>(topicQuery, 1, 2);
topicQuery.OrderBy.Remove(critSort);

//For Latest replies we want to sort topics by LastReply.CreateDate. Therefore we add this. 
topicQuery.LastReply = new ReplyCriterion();
topicQuery.Replies = new ReplyCollectionCriterion();
topicQuery.Replies.Count = new IntegerCriterion();
topicQuery.Replies.Count.Operator = ComparisonOperator.GreaterThan | ComparisonOperator.Equals;
topicQuery.Replies.Count.Value = 1;
topicQuery.LastReply.CreateDate = new DateTimeCriterion();
CriterionSortOrder critSort2 = new CriterionSortOrder(topicQuery.LastReply.CreateDate, EPiServer.Common.Sorting.SortingDirection.Descending);

topicQuery.OrderBy.Add(critSort2);
MessageCollection replies = QueryHandler.GetQueryResult<EPiServer.Community.Forum.Topic, MessageCollection>(topicQuery, 1, 4);

lvMyEntries.DataSource = topics;
lvMyEntries.DataBind();
lvMyReplies.DataSource = replies;
lvMyReplies.DataBind();

The key point here appears to be the use of the IntegerInCriterion which I'm inferring from the name allows you submit a set of integers to be used in the query in a criterion (like a SQL IN clause I guess).

OTHER TIPS

Try using EPiServer.Community.Forum.ForumHandler.GetQueryResult() and its overloads.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top