Moses' Blog

Living {.net} lifestyle

Muhammad Mosa

Moses' profile picture
Logo
Software Engineer.
MCT, MCSD.NET,
MCTS: .Net 2.0 Web, Windows, Distributed Applications
MCTS: .Net 3.5 WF Application Development
MCTS: WSS 3.0, MOSS 2007 Configuration & App Dev
MCPD: Enterprise Application Developer

Send mail My Live Space Moses on Facebook Twitter Moses on Technorati

Sponsors



Calendar

<<  January 2009  >>
MoTuWeThFrSaSu
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar

Community Credit

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

BlogEngine.Net Extensions How-To part 01, Simple Post View Count Extension

In this post and following one, I'll explore how to build BlogEngine.Net Extension. In my previous post I showed a simple Extension. Here I'll start again with how to build simple Extension to view total view counts of a post.

Step 1 - Create your class and mark it with Extension Attribute:
Just in the App_Code/Extensions folder, create your extension class and mark it with Extension attribute just as the following:

   1: [Extension("Counts and display number of views for a post", "1.0", "<a href=\"http://www.mosesofegypt.net\">Moses</a>")]
   2: public class PostViewes
   3: {
   4: }

The Extension attribute is very simple, it accepts 3 parameters, Description of the Extension, Extension version and the name of the Author who developed this extension. All are strings of course.

Step 2 - Implement Extension Constructor to define event handlers:
As I mentioned in my previous post, Extension mainly used to handle predefined BlogEngine events such as saving post, serving post, adding comment etc... To view all available list of events click here.

In my sample we will handle only one event which is Post.Serving. The event is taking place when ever a post is being served to the client. Like when viewing it or when view posts list.

   1: public PostViewes()
   2: {
   3:     Post.Serving += new EventHandler<ServingEventArgs>(OnPostServing);
   4: }

Step 3 and final - Implement the event handler
Time to code, this is the heart of your extension; the implementation of the event handlers. It is interesting to know the signature of the Post.Serving Event:

private void OnPostServing(object sender, ServingEventArgs e)
  1. object sender Parameter: here will be an object of type IPublisher specifically an instance of a Post
  2. ServingEventArgs e: an event argument containing useful information about the post such as its body and serving location.

I'll explore 2 properties of ServingEventArgs:

  1. Location property: An enumeration of type ServingLocation. Actually a post can be served in many locations. In the main page as part of post list and single by viewing the post itself alone. Also it can be through an RSS feed. We will count the views based on SinglePost view. But will display total views on both PostList and SinglePost.
  2. Body property: The body that is going to be rendered to the client. Safely you can override the value of property without affecting the real content of the post. That means your modifications to this property will not be stored permanently on your storage. We will use this property to display a text at the end of the post showing total number of post view in both SinglePost location and PostList location.
private void OnPostServing(object sender, ServingEventArgs e)
{
    IPublishable ipub = ((IPublishable)sender);
    //Check For Single Post View, When viewing Specific Post, basically through post.aspx
    if (e.Location == ServingLocation.SinglePost)
    {
        if (File.Exists(_FileName))
        {
            int viewCount;
            
            //Fetch out total views of current viewing post.
            Dictionary<string, int> posts = IncrementPostViewCount(ipub.Id.ToString(), out viewCount);
            
            //Save list of posts in Xml File with new counted post.
            WriteToXml(posts);
 
            //Override the body of the post (temporary) to display total views
            e.Body += String.Format("<br/> Views({0})", viewCount);
        }
    }
    //Check For Post List View, basically through main default.aspx or using postlist.ascx usercontrol
    else if (e.Location == ServingLocation.PostList)
    {
        int viewCount = GetPostViewCount(ipub.Id.ToString());
        
        //Override the body of the post (temporary) to display total views
        e.Body += String.Format("<br/> Views({0})", viewCount);
    }
}

There are 3 helper method used in this extensions basically to read and write from XML file. I used an XML file to store post id along with total number of views. Values are overridden on each post serve to keep track of latest view counts.

In my next part I'll enhance this extension with extra settings like enable the display of the total views only when use is authenticated and apply some styles. This should be applied by adding settings to the Extension that can be edited by the blog administrator.

Download Extension source (1.76 kb) to review the code.

kick it on DotNetKicks.com

Posted: Mar 01 2008, 02:26 by mosessaur | Comments (4) RSS comment feed |
  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: ASP.NET | BlogEngine.Net
Social Bookmarks: E-mail | Kick it! | DZone it! | del.icio.us

Comments

Add comment


(For Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading