I'm creating a topics list for a forum where I want to show how many times each topic has been read/viewed. I'm using the Topic.NumViews property to do this, but for some reason it returns 0 views for all topics, why?
This property is not incremented automatically by the framework, since each solution may have its own definition of when the view count should be increased.
This means that you have to increment it yourself by calling this method of the forum handler:
I would like to point out that if you are using the latest version of EPiServer Community (3.2) then you should use the VisitHandler instead since the NumViews will be obsolete in the future.
I tried the VisitHandler to add visits (with Topic.GetNumVisits() to get the value) and now it works as expected. However, users should be able to re-sort the topic list by the number of views. For other sort options I've used TopicSortOrder with various TopicSortField values, but when I try TopicSortField.Views it doesn't seem to work (I guess my NumViews values are still 0 because I don't use the IncrementViews method?)
Is there a way of sorting the topics by number of visits?
If you are using the "Visit-system" - which is the way to go - you should not use the NumViews, since it's obsolete.
Presuming you have used VisitHandler to add the visit to the topic:
VisitHandler.AddVisit(new Visit(currentTopic, new UserAuthor(currentUser)));
Then you can use the VisitHandler to get the visited items and sort them the way you want:
int page = 1;int pageSize = 10;int totalItems;VisitableEntitySortOrder order =new VisitableEntitySortOrder(VisitableEntitySortField.NumVisits, SortingDirection.Descending);VisitableEntityCollection visitedItems =VisitHandler.GetVisitedItems(typeof (EPiServer.Community.Forum.Topic), page,pageSize, TimeSpan.FromMinutes(10), out totalItems, order);
I hope this helps!