Add Google Plus One Extension to BlogEngine.NET

A few years ago I built my personal website using BlogEngine.NET, every once in a while I tend to pay attention to the site and “freshen it up”.  Back in December of 2011, I upgraded to version 2.5 of BlogEngine.NET and updated the theme.  Last night I added facebook like buttons to all those post and pages courtesy of isharpnote. Well I wanted to add a Google Plus One button to my site also but I could not find any BlogEngine.NET extensions for it, so what does every developer do, create one.  I started with the “Facebook Like with Google Plus Extension for BlogEngine 2.5” extension from isharpnote and tweaked it for my needs.

Creating the extension

To create a BlogEngine.NET extension you need a class and attribute it with the BlogEngine.Core.Web.Extensions.Extension attribute, like so:

Parameters explained

Parameters Explained
Parameter NameUsed for
DescriptionA description of your extension
VersionThe version of your extension
AuthorA link to the author of the extension
PriorityThe priority of the extension, in relation to others.

This class will need to reside in the App_Code\Extensions folder of your site.

Next step, if you want to have settings, you will need to tell the extension manager that you have settings.  You do this by executing code similar to this, in the constructor of your extension or a method that is called in the constructor of your extension.

In my extension, I call a method called InitializeSettings in the constructor

You’ll notice that I attached two events, Post.Serving and Page.Serving, to the ServingHandler method. This will tell BlogEngine.NET that I want to know when it is serving up pages or blog posts.

Handling the page and post serving

On line 3 you’ll not the check to see if the extension is enabled.  Believe it or not, people might turn off your extension.

Line 4 checks to see if this is in a feed if so, we do not want to apply the button to the page or post.

Line 6 – 10, I get a reference to the page so that I can inject the required JavaScript for the Google Plus One button.

Next, I get a reference to the post and append to the body of the post the Google Plus One button.

That’s it. You can download the complete extension here GooglePlusOne.cs

Leave a Reply