Building A BlogEngine Extension, ModeratedComments Extension

by mosessaur| 26 February 2008| 10 Comments

If you are BlogEngine.Net fan, then you should heard or used BlogEngine.Net Extensions; the extensibility feature in BlogEngine.Net. My story with Extensionsstarted just last week. I enabled the comment moderation setting on my blog. And I faced a small issue, I wanted a page to view all moderated comments -waiting for approval-. I proposed that to my friend Amr, he suggested to me to have a look at BlogEngine.Net Extensions

The Story:
Well, the story was that I wanted to view all unapproved comments on my blog. There was only one solution in my mind before Amrsuggest to me to use BlogEngine.Net Extensions. That solution was to add new methods to exiting APIs or use the existing ones. I liked the idea of using existing ones, but the issue is that I will have to loop through all posts to find out if any as unapproved comments. And of course I didn't like the idea of adding new methods to existing APIs because it is just not acceptable at the time being, I am not one of BlogEngine developer's team, so will be hard for me to integrate my code in the upcoming releases.

The Extension Solution:
"Extensions allows you to write a class that can hook up to all the various events that are exposed in the application in a very simple manor" -Creating Extensions on BlogEngine Wiki-. Yes this sound like something to me. I need every time a comment is added to my blog to bookmark the post as it has pending comments waiting for approval. So I will handle Post.CommentAdded event and bookmark the post. Later, I'll build a page to view bookmarked posts and retrieve their moderated comments. To Bookmark a post, I just used simple XML file to store the unique identifier of the post.

How to build simple BlogEngine Extension:
A simple BlogEngine Extension, is an extension without any parameters. It is used as it is, you do not modify it's settings. To build such simple one you only have to do 4 simple steps:

  1. Create a class and mark it with Extension Attribute.
  2. In the constructor of your class, register event handlers for the events you wish to handle.
  3. Implement -code- your event handlers
  4. Put your finished class file in the App_Code/Extensions folder of your web application.

ModeratedComments Extension:
The ModeratedComments Extension I am proposing here handles 3 events:

  1. Post.CommentAdded
  2. Post.CommentRemoved
  3. Comment.Approved

The first event handler will just bookmark the post -put it in the XML file- if it is not already bookmarked. The 2nd and 3rd handlers will make sure that the post -if bookmarked- still have moderated comments, otherwise it will remove the post from the bookmark list.

Declare Extension Class:

   1: [Extension("Register posts with moderated comments. Can be viewed from <a href=\"../Pages/ModeratedComments.aspx\">Moderated Comments Page</a>", "1.0", "<a href=\"http://www.mosesofegypt.net\">Moses</a>")]
   2: public class ModeratedComments
   3: {//.....
   4: }

Register Event Handlers in Constructor:

   1: public ModeratedComments()
   2: {
   3:     Post.CommentAdded += new EventHandler<EventArgs>(OnCommentAdded);
   4:     Post.CommentRemoved += new EventHandler<EventArgs>(OnCommentRemovedOrApproved);
   5:     Comment.Approved += new EventHandler<EventArgs>(OnCommentRemovedOrApproved);
   6: }

Event Handlers Code:

   1: private void OnCommentAdded(object sender, EventArgs e)
   2: {
   3:     Comment comment = (Comment)sender;
   4:     if (!comment.IsApproved)
   5:     {
   6:         IPublishable ipub = comment.Parent;
   7:         StringCollection ids = FillIds();
   8:         //Check if Post Id is not in the bookmark list
   9:         if (ids != null && !IdExists(ipub.Id.ToString(), ids))
  10:         {
  11:             //Not bookmarked add it to bookmark
  12:             ids.Add(ipub.Id.ToString());
  13:             //Save bookmark list -XML File-
  14:             WriteToXml(ids);
  15:         }
  16:     }
  17: }
  18: private void OnCommentRemovedOrApproved(object sender, EventArgs e)
  19: {
  20:     Comment comment = (Comment)sender;
  21:     IPublishable ipub = comment.Parent;
  22:     if (ipub is Post)
  23:     {
  24:         //Check if post still have moderated comments
  25:         if (((Post)ipub).NotApprovedComments == null || ((Post)ipub).NotApprovedComments.Count == 0)
  26:         {
  27:             StringCollection ids = FillIds();
  28:             if (ids != null && IdExists(ipub.Id.ToString(), ids))
  29:             {
  30:                 //No more moderated comments and the post is in bookmark list
  31:                 //then remove it.
  32:                 ids.Remove(ipub.Id.ToString());
  33:                 WriteToXml(ids);
  34:             }
  35:         }
  36:     }
  37: }

This way the extension is completed. In addition, I've built a page and placed it in the admin folder. This page is very simple, it only read the bookmark file, retrieve bookmarked posts with their moderated comments and display them on a grid.
moderatedcomments

Download ModerateComments Extension (3.31 kb)

kick it on DotNetKicks.com

Comments (10) -

Janko
Janko on 5/16/2008 8:03 AM Moses, you are doing a great job with blogengine extensions. I'll use some of them in the next version of my blog Smile
mosessaur
mosessaur Egypt on 5/17/2008 3:51 PM Thank you so much Janko. I to your blog running using BlogEngine as it is really great Blogging Engine.
Daniel
Daniel Mexico on 6/9/2008 12:22 AM gracias por su valiosa información
Sohbet
Sohbet on 6/10/2008 1:47 AM Thank You
Wayne
Wayne United States on 7/15/2008 7:48 PM Ah, my new best friend.  Hello there, nice to meet ya.  I'll be interested in seeing what you come up with.  I'm using BE on my site, and still trying to determine exactly what I want on my site in terms of extensions.  Have anything in the works that you can share with us?  I'm might be able to perform guinea pig tricks for ya.

Great post!
mosessaur
mosessaur Egypt on 7/15/2008 8:48 PM Thank you Wayne actually there are a lot of extensions around, and all are posted on http://www.dotnetblogengine.net/
You could check post view counter extensions too on this blog.
Busby SEO Test
Busby SEO Test United States on 11/22/2008 12:29 PM Nice tips about Building A BlogEngine Extension, ModeratedComments Extension
Online free games
Online free games Serbia on 1/19/2009 6:30 AM great article mate. i think blog engine is better than wordpress.
Free Games
Free Games United States on 3/3/2009 9:40 PM This is a really good post. The blog engine sounds awesome.
my blogging net
my blogging net United States on 6/25/2009 12:34 AM I want to have additional information using HTML, Java, CSS for my blog site. And finding what themes are applicable to my blog site and choosing what is the best version of blogengine for my blog site.

Pingbacks and trackbacks (1)+

Comments are closed