Moses' Blog

Living {.net} lifestyle

Away Notice

I'll be away for the next 19 days starting from 30th Sept. I have no internet connection during this period.

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

<<  October 2008  >>
MoTuWeThFrSaSu
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar

Recent Comments

Comment RSS

Community Credit

Disclaimer

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

© Copyright 2008

Building A BlogEngine Extension, ModeratedComments Extension

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

Posted: Feb 27 2008, 01:00 by mosessaur | Comments (7) RSS comment feed |
  • Currently 0/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