BlogEngine.Net Extension Base Class Generator

As I had already hinted in my previous post, I was creating a program that would ease the process of creating extension for BlogEngine.Net. The advantage of this program is that you don’t really have to know everything about BlogEngine.Net Core. Most of the work is done by the program. The program automatically generates the code for settings including initializing it.

Please note that this program is not totally complete. The program generates very limited code and therefore I cannot say that it’s a complete solution. But as far as my needs were concerned, it did fulfil it.

So let me show you how this program works by showing you how to create an extension using this program. Before you start following the steps, let me tell you what the extension will do.

The extension will insert the HTML, you provide through settings page, at the bottom of the posts. So let me show you how to use this program by showing you to create a basic extension.

  • Download the program from the below given link and extract the exe to the desired location.
  • Run the program from that location.
  • As you can see, the program is divided into three sections.

Empty

  • Basic Info, Setting Details & Settings List
  • Add the Basic Info as depicted in picture below

Basic_Info

  • Also add the Setting Details as given below.

Signature_Text

  • Now press Add button. The setting should get listed in the table given on the right.
  • Again enter the Setting Details as given in the image below and press Add

Restrict to single post

  • Now you should have basic information about your extension plus the settings you need for the extension listed on the right grid.

Full

  • That’s it. Now go to Action menu and click on Build Code.

Build Code

  • A window will open the core code which is given below.
#region Using

using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;

#endregion


[Extension("Adds a signature text at the end of the post.", "1.0", "Mayank Raichura")]
class Signature
{
    #region Declarations.
    private static ExtensionSettings settings;
    private static Post post;
    #endregion

    public Signature()
    {
        InitSettings();
        Post.Serving += new EventHandler<ServingEventArgs>(Post_Serving);
    }
    private void Post_Serving(object sender, ServingEventArgs e)
    {
        post = (Post)sender;
        if (post == null)
            return;
        settings = ExtensionManager.GetSettings(GetType().Name);
        //Settings should be retrieved after this...
        string sigSignature_Text = settings.GetSingleValue("Signature_Text").ToLower();
        bool sigRestrict_to_single_post = bool.Parse(settings.GetSingleValue("Restrict_to_single_post").ToLower());

        //End Settings Retrieval
        e.Body += "\n<!-- Signature extension by Mayank Raichura -->\n";
        e.Body += BuildHTML(sigSignature_Text, sigRestrict_to_single_post);
    }
    string BuildHTML(string sigSignature_Text, bool sigRestrict_to_single_post)
    {
    }

    private void InitSettings()
    {
        {
            ExtensionSettings initialSettings = new ExtensionSettings(GetType().Name);
            initialSettings.IsScalar = true;

            string htmlHelp =
            "<strong>Signature Text:</strong> The HTML that will be injected at the bottom of the post. You can even insert javascript.<br />"
            + "<strong>Restrict to single post:</strong> Check this if you want to restrict signatures to full post view only.<br />"
            ;


            initialSettings.AddParameter("Signature_Text", "<br /><br /><strong>Signature Text</strong>", 50);
            initialSettings.AddParameter("Restrict_to_single_post", "<br /><br /><strong>Restrict to single post</strong>", 50);


            initialSettings.AddValue("Signature_Text", "Regards<br />Mayank Raichura");
            initialSettings.AddValue("Restrict_to_single_post", true);

            ExtensionManager.ImportSettings(initialSettings);
        }
    }

}

 

  • Please observe that all the major work has been done by the program. In normal case, the only code you will need to modify will be the BuilHTML function which is currently empty. Ofcourse you can write other functions and call them in BuildHTML. But what’s best is that you don’t have to go through the API for the extension.
  • Now to maintain brevity I’m not going into how to implement the Restrict to full post feature but will directly to insert the signature to the post. Infact, it’s a piece of cake in here.
string BuildHTML(string sigSignature_Text, bool sigRestrict_to_single_post)
{
	return sigSignature_Text;
}
  • You are done. Copy the code to a text file, save it as a .cs file and copy it to your extensions directory in ~/AppCode/Extensions/ and your extension should be visible in the extensions page.

Possible Enhancements

  • Add support for DataTable (Non-Scalar) Settings
  • Add support for other events like Page.Serving, Comment.Serving and many more
  • Save/Load project.

A/N: This extension is no where near a full-blown program to create extensions of all kind. But I’ll keep updating it as and when I find time to add extra features.

BlogEngine Extension Base Class Creator.zip

Your comments & reviews are always welcome…..:)

Website Security Test