NumViews property returns 0?

Member since: 2006


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?

Best regards,


#27514 Feb 02, 2009 9:46
  • Member since: 2007

    Hi Helena,

    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.

    Best regards,

    #27516 Feb 02, 2009 10:26
  • Member since: 2006

    Thanks Tom!

    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?

    Best regards,

    #27528 Feb 02, 2009 11:48
  • Member since: 2007

    Hi again,

    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!

    Best regards,

    #27534 Edited, Feb 02, 2009 13:14
This thread is locked and should be used for reference only. Please use the Legacy add-ons forum to open new discussions.
Please login to post a reply