This is the first part in the series of tutorials about writing extensions for the BlogEngine 1.4. I’ll start with simple “hello world” example and then gradually move to the more advanced techniques. You don’t need to have any previous experience with BE extensions to follow this tutorial.

BlogEngine extension is just a class file that you can drop in the App_Code folder. That means, you can use any editor to write it, from Notepad to full blown Visual Studio. Express edition also works fine. For VS, you can start a new class library, set reference to BlogEngine.Core.Dll and you’ll get all VS functionality enabled. Obviously, you can download complete source code from CodePlex repository and use it as your project, if fact this is a preferable method as you’ll be able to brows files easier. You can use VS 2005 or convert solution to VS 2008. Whatever your choice is, create a new file, call it Example_01.cs then copy and paste code shown below.

using System;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core;
 
[Extension("Example_01", "1.0", "Me")]
public class Example_01
{
    public Example_01()
    {
        Post.Serving += new EventHandler<ServingEventArgs>(ServingHandler);
    }
 
    private void ServingHandler(object sender, ServingEventArgs e)
    {
        e.Body = e.Body.ToLower();
    }
}

This example is very simple, let’s go over it to understand what it is doing. First of all, we need to import Core libraries to get access to BE objects like Post, Page etc. with “using” statement. Then, we need to decorate our class with “Extension” attribute. BlogEngine will examine class attributes on application start up and, if class is marked as extension, it will create instance of this class and hook up the event handlers which is vital for extension to work. Attribute has three properties: extension name, version, and author. Extension must have a default public constructor without parameters, this is where we subscribe to Post.Serving event (list of events can be found here). Now, every time this event fired (Post is served to the web browser) function ServingHandler will be executed.

public static void OnServing(Post post, ServingEventArgs arg)

ext-14-5

This function takes two arguments: Post and ServingEventArgs. ServingEventArgs has two important properties: Body, the string representing content of the post, and Location. Location can tell you what kind of post been served. When you use Post.Serving it is either PostList or SinglePost. Body gives you access to content of the post, so you can easily manipulate it. In this example, we change post content to lower case. Copy Example_01.cs to the App_Code folder in the working BlogEngine web site and go to the front page. IIS will automatically restart when it finds new class in this folder, reloads application and extension will become part of the web site. When page is loaded, you should see each post in lower case. Not very useful at this point yet, but as you can see it is very easy to write and use BE extension.

ext-14-4

ext-14-3

If you click Extensions link under Administration menu, it will bring you to the list of all extensions used by your blog. The new extension, Example_01, will be listed here too. Version, description and author taken straight from the extension attribute in the class we just created. You can enable or disable extension (you’ll need ASP.NET user have write permissions on App_Code folder for this to work). You can also look at extension’s source code if you click “View” link under Source column in the list. Our simple extension does not have settings, so you don’t see “Edit” link next to the extension name.

ext-14-1

ext-14-2

In the next tutorial we examine what extension settings are, why you (often) need them and how to use Extension Manager to manage extensions and  settings in the BlogEngine 1.4.

Signature

Yesterday newly installed Firefox 3 crashed on me at least 10 times, which, I believe, is a new world record (ha-ha). It could be because I am visiting a lot of Microsoft-centric sites that often run Silverlight. It looks like Silverlight 1.0 does not work in FF3 at all and Silverlight 2.0 kills it. Or vice-versa. Lovely... At the end, I had to drop to IE 6 (tough without tabs, but works fine). So, where does it put Silverlight and who is to blame? Specially considering the fact that problem with compatibility was known long ago and existed since early betas?

It doesn’t matter, everybody is a looser. Firefox – because it is crashes. Microsoft – because it’s cross platform/cross browser technology did not cross anything yet (very limited on Linux, still not working in Opera despite promises etc.). Average Jo – because “internet is broken”.

Signature

A lot of "power bloggers", prefer to use Windows Live Writer instead of built-in web editor. No matter how good web editor is, desktop publishing is just too hard to beat, at least at the moment. I personally like WLW myself and use it whenever I can. But this is not the only choice you have on the desktop; another big one is MS Word 2007. I'm not an Office guru, but if you watch the video at the end of this post you can get ideas how easy and intuitive content creation can be when you have the right tool at your disposal. You can set it up with BlogEngine just as easy as WLW. Simply open new document and go to Publish/Blog in the office menu. More...

Signature

Preparing for BE 1.4 release, which is coming soon (although exact date not yet set), I started working on documentation for Extension Manager and changes it has compared to previous BE version. This is a short list I came up with. Obviously, I'll have to go into specifics and provide code examples for documentation be useful. This will be on the Wiki as soon as 1.4 released. More...

Signature

We all know about magic App_Code folder. Just drop class file in there and it will become a part of the web application. This is fine for scripts like PHP or "classic" ASP (VB script), but C# is strongly typed compiled language. How App_Code works? As any magic, mostly smoke and mirrors. Behind the scene, ASP.NET will create App_Code.dll and merge it with main application assembly at run time. This simple trick gives us best of both worlds - dynamism of scripting languages (ok, to the point) and all the good stuff coming with strong typing and compilation (whatever they are). More...

Signature

For Windows developers trying to diversify their skills and go Mono, setting up environment is probably the most painful experience. We spoiled by Microsoft tricked us into believe that setup and configuration done by repeatedly pressing “ok” button. It is not so in the wild world outside MS sandbox. For those bold and curious, as a first step I recommend another sandbox – VMware. Try it, see if you like it and, if not – not a big deal, just remove virtual image and pretend it never happened. No hard feelings. More...

Signature

Small little tiny but mostly good things. More...

Signature

I use thumb flash drives quite a lot moving between computers. It is still much faster to load a gig or two from the flash drive than to use Skydrive or other "cloud" alternative. For the most part it is painless - stick it in and OS will find and mount it, no setup requires. But one day perfectly good USB drive stopped working all of the sudden on one specific computer. It works fine on the others and other USBs work fine on that computer too. What's going on? More...

Signature

Crafting CSS style sheet

This is the most time consuming operation in porting any CSS template to BlogEngine. If you like your design pixel perfect - that will cost you. These are some general tips that I hope will help make job a little bit easier. More...

Signature

Cool thing about open source is sharing. You give some - you get some. When it comes to themes, there are tons of great free designs out there on the web for applications like blogs. Some of them are generic CSS templates, others specifically designed for popular open source projects like WordPress. This tutorial is about converting WordPress theme to BlogEngine, but most of it very much applied to almost any web template in the universe. More...

Signature
<<  July 2008  >>
SuMoTuWeThFrSa
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
Enhanced with Snapshots

Subscribe to Rtur.net