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.
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:
- Create a class and mark it with Extension Attribute.
- In the constructor of your class, register event handlers for the events you wish to handle.
- Implement -code- your event handlers
- Put your finished class file in the App_Code/Extensions folder of your web application.
The ModeratedComments Extension I am proposing here handles 3 events:
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
Register Event Handlers in Constructor:
1: public ModeratedComments()
3: Post.CommentAdded += new EventHandler<EventArgs>(OnCommentAdded);
4: Post.CommentRemoved += new EventHandler<EventArgs>(OnCommentRemovedOrApproved);
5: Comment.Approved += new EventHandler<EventArgs>(OnCommentRemovedOrApproved);
Event Handlers Code:
1: private void OnCommentAdded(object sender, EventArgs e)
3: Comment comment = (Comment)sender;
4: if (!comment.IsApproved)
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))
11: //Not bookmarked add it to bookmark
13: //Save bookmark list -XML File-
18: private void OnCommentRemovedOrApproved(object sender, EventArgs e)
20: Comment comment = (Comment)sender;
21: IPublishable ipub = comment.Parent;
22: if (ipub is Post)
24: //Check if post still have moderated comments
25: if (((Post)ipub).NotApprovedComments == null || ((Post)ipub).NotApprovedComments.Count == 0)
27: StringCollection ids = FillIds();
28: if (ids != null && IdExists(ipub.Id.ToString(), ids))
30: //No more moderated comments and the post is in bookmark list
31: //then remove it.
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.
Download ModerateComments Extension (3.31 kb)