More in this section

Forums / Developing with Sitefinity / Blog View Counter

Blog View Counter

6 posts, 0 answered
  1. Brook
    Brook avatar
    14 posts
    Registered:
    27 Oct 2009
    28 Mar 2008
    Link to this post
    I am looking for some guidance on the best method to implement a Blog Post View counter and a PostList View counter.  Can this be a simple extension of the current Blog Module or do I need to creat a cuastomer deriviative?

    I want to use the View Counter rank posts by most viewed. 

    I am also looking at adding a rate this post feature.  Anyone done either of these yet?
  2. Kalina
    Kalina avatar
    176 posts
    Registered:
    27 Oct 2016
    01 Apr 2008
    Link to this post

    Dear Brook,

    I am sorry for the delayed response.

    Our technical people are reviewing your request currently and will provide a suggested resolution tomorrow.

    Regards,
    Kalina
    the Telerik team


    Instantly find answers to your questions at the new Telerik Support Center
  3. Vassil Daskalov
    Vassil Daskalov avatar
    261 posts
    Registered:
    18 May 2013
    02 Apr 2008
    Link to this post
    Hello Brook,

    Here is a sample solution for achieving the desired behavior.
    You will need to override the SinglePost control and add one additional meta field to hold the views count. Below is the sample implementation of the overridden SinglePost control. You need to override the Render event and increment the views count there to be sure that the control will actually be displayed. We should also check if the Page object does not inherit EditPage or PreviewPage classes as this would mean that the control is either in edit or preview mode and incrementing the count in this case would be inappropriate.
    Here goes the code:

    public class SinglePostUpdate : SinglePost  
    {  
        protected override void Render(System.Web.UI.HtmlTextWriter writer)  
        {  
            base.Render(writer);  
     
            if (!(this.Page is EditPage) && !(this.Page is PreviewPage))  
            {  
                BlogManager manager = new BlogManager(this.ProviderName);  
                IContent post = manager.Content.GetContent(this.PostID);  
     
                if (post != null)  
                {  
                    object o = post.GetMetaData("ViewsCount");  
                    if (o != null)  
                    {  
                        long postViews = (long)o;  
                        postViews++;  
                        post.SetMetaData("ViewsCount", postViews);  
                    }  
                }  
     
                manager.Content.SaveContent(post);  
            }  
        }  

    In the web.config file, define the new meta field called ViewsCount and register the newly created control like this:

    <cms defaultProvider="Sitefinity" pageExtension=".aspx" disabled="false" 
       pageEditorUIMode="Overlay">  
        ...  
        <toolboxControls> 
            <clear /> 
            ...  
            <add name="Custom Single Post" section="Blogs" type="BlogsUpdate.SinglePostUpdate, BlogsUpdate" /> 
        </toolboxControls> 
    </cms> 
     
    ...  
     
    <cmsEngine defaultProvider="Generic_Content">  
        ...  
        <metaFields> 
            ...  
            <add key="Blogs.ViewsCount" valueType="Integer" visible="True" searchable="True" sortable="True" defaultValue="0"/>  
            ...  
        </metaFields> 
    </cmsEngine> 

    The last thing is to show the post views to the clients. You need to modify the ~/Sitefinity/ControlTemplates/Blogs/SinglePostTemplate.ascx by adding a Label bound to the meta field:

    <strong>Views: </strong> 
    <asp:Label runat="server" Text='<%# Eval("ViewsCount") %>'></asp:Label> 

    Hope this would do the job. Let me know if I am missing something.

    Best wishes,
    Vassil Daskalov
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  4. Vlad
    Vlad avatar
    498 posts
    Registered:
    19 Jun 2017
    16 Sep 2008
    Link to this post
    Hello,

    Here is an update of this solution. The difference is only with the implementation of the SinglePostUpdate control:

            public class SinglePostUpdate : BlogPosts 
            { 
                protected override void Render(System.Web.UI.HtmlTextWriter writer) 
                { 
                    base.Render(writer); 
     
                    if (this.Page is InternalPage && this.itemId != Guid.Empty) 
                    { 
                        IContent post = this.Manager.GetContent(this.itemId); 
     
                        if (post != null
                        { 
                            object o = post.GetMetaData("ViewsCount"); 
                            if (o != null
                            { 
                                long postViews = (long)o; 
                                postViews++; 
                                post.SetMetaData("ViewsCount", postViews); 
                            } 
                        } 
                        this.Manager.SaveContent(post); 
                    } 
                } 
            }   


    Best wishes,
    Vlad
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  5. ddblee
    ddblee avatar
    20 posts
    Registered:
    24 Jul 2008
    25 Sep 2008
    Link to this post
    In the sample code you provide for the overriding of SinglePost, can you list the "using" statements that would be needed at the top of this code?

    Thanks!
  6. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    26 Sep 2008
    Link to this post
    Hi Lee,

    Here are the using statements:

    using System; 
    using System.Data; 
    using System.Configuration; 
    using System.Web; 
    using System.Web.Security; 
    using System.Web.UI; 
    using System.Web.UI.HtmlControls; 
    using System.Web.UI.WebControls; 
    using System.Web.UI.WebControls.WebParts; 
    using Telerik.Blogs.WebControls; 
    using Telerik.Cms.Web; 
    using Telerik.Cms.Engine; 
     
    public class SinglePostUpdate : BlogPosts 
      ... 

    The namespaces which are specific for Sitefinity are marked in green.

    Hope this helps.

    Kind regards,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Register for webinar
6 posts, 0 answered