Creating widgets for BlogEngine.NET 1.4

Jul 2, 2008

The new BlogEngine.NET 1.4 let’s you easily create and distribute widgets like the ones you see on the right hand side on this page. Widgets are basically just small pieces of content and you can leverage the entire .NET Framework to create them. They can pull data from BlogEngine.NET itself or from external sources like web services or remote XML files. Everything is allowed and possible.

Create a widget

In this guide we will create a widget called Most comments and you can see it live on this site where it is called Hall of fame. The widget lists the people who have written most comments in the last x number of days.

 

You start by creating a new folder in the widgets folder. The name of the folder will be the name of the widget as well. In the new folder you have to add a user control called widget.ascx. If the widget contains settings that the blog owner should be able to change, then you have to add another user control called edit.ascx. It’s important that you name them exactly widget.ascx and edit.ascx.

 

Let’s start with widget.ascx. This is the user control that displays the widget content on the blog. The first thing you need to do is to change the inheritance of the code-behind file from System.Web.UI.UserControl to WidgetBase. Then you need to override two properties and one method.

The properties only have getters and they look like this.

/// <summary>

/// Gets the name. It must be exactly the same as the folder that contains the widget.

/// </summary>

public override string Name

{

  get { return "Most comments"; }

}

 

/// <summary>

/// Gets wether or not the widget can be edited.

/// <remarks>

/// The only way a widget can be editable is by adding a edit.ascx file to the widget folder.

/// </remarks>

/// </summary>

public override bool IsEditable

{

  get { return true; }

}

The method to override is called LoadWidget and you should use it just as you would have used the Page_Load event handler. In other words, this is where you put all the code that needs to run when the widget loads every time a page is requested. If the widget can be edited then this is where you would retrieve the settings that BlogEngine.NET stores for each individual widget. The settings are stored in a simple StringDictionary.

StringDictionary settings = GetSettings();

The widget framework handles the caching of the settings, so you don’t have to worry about that.

Edit a widget

If the property IsEditable on the widget.ascx is set to True, the widget will render an edit link on the top right corner of the widget. When the edit link is clicked, an iframe popup shows up and loads the second user control edit.ascx. From there you can change the settings and save them.

The edit.ascx you create yourself doesn’t handle the title of the widget or whether or not the title show be shown. That is handled by the widget framework for you. The same goes for the save button.

You only have to add the controls needed to change what is specific to your new widget. In the case of the Most comments widget, it’s a checkbox and three text boxes. The first you need to do is to change the inheritance of the code-behind file from System.Web.UI.UserControl to WidgetEditBase. Next you have to override a method called Save, which get’s called by the widget framework when the save button is clicked. It saves the settings entered by the user.

public override void Save()

{

  StringDictionary settings = GetSettings();

  settings["avatarsize"] = txtSize.Text;

  settings["numberofvisitors"] = txtNumber.Text;

  settings["days"] = txtDays.Text;

  settings["showcomments"] = cbShowComments.Checked.ToString();

  SaveSettings(settings);

}

When you bind the settings to the controls, you have to use the Page_Load or similar methods just as you are used to. A good idea is to load the default values if the settings haven’t been set yet.

protected override void OnPreRender(EventArgs e)

{

  if (!Page.IsPostBack)

  {

    txtNumber.Text = "3";

    txtSize.Text = "50";

    txtDays.Text = "60";

    cbShowComments.Checked = true;

 

    StringDictionary settings = GetSettings();

    if (settings.ContainsKey("avatarsize"))

      txtSize.Text = settings["avatarsize"];

 

    if (settings.ContainsKey("numberofvisitors"))

      txtNumber.Text = settings["numberofvisitors"];

 

    if (settings.ContainsKey("days"))

      txtNumber.Text = settings["days"];

 

    if (settings.ContainsKey("showcomments"))

      cbShowComments.Checked = settings["showcomments"].Equals("true", StringComparison.OrdinalIgnoreCase);

  }

}

You have total freedom to create the edit page just as you like with JavaScript, stylesheets or whatever you like. It’s loaded asynchronously when the edit link is clicked and not before.

Download

Download the zip file below and extract the Most comments folder into your widget folder.

Most comments.zip (4,00 kb)

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Comments (19) -

Juan
Juan Argentina
7/2/2008 6:39:27 PM #

Cool. I migrated to 1.4 yesterday and created a few widgets for (for example) displaying Google Reader's shared items...

It was very easy to do, I love the new Architecture... I'll upload it later

Domenic
Domenic United States
7/2/2008 8:22:06 PM #

I feel like the settings should use a databinding pattern... this whole Save/OnPreRender thing is pretty crude Frown. Maybe it's possible to integrate that into what we have though... actually that seems fairly plausible, hmm.

Troy Goode
Troy Goode United States
7/2/2008 9:34:41 PM #

Any word on a central hosting area for widgets, maybe on dotnetblogengine.net? A list ala what you've done with extensions would even be better than nothing.

Josh Stodola
Josh Stodola United States
7/3/2008 12:10:23 AM #

This new widget framework is fantatsic. The flexibility and freedom of it is the best part. WordPress, eat your heart out.

Hey, look, I'm in the hall of fame!

Justin Etheredge
Justin Etheredge United States
7/3/2008 2:36:05 AM #

Very nice, I am totally going to have to look into building some plugins for 1.4!

Mads Kristensen
Mads Kristensen United States
7/3/2008 5:52:23 AM #

@Troy,

The community site will launch very soon and from there you will be able to share and download widgets, extensions and themes.

Cristiano
Cristiano Italy
7/3/2008 7:06:29 AM #

Top Commenters Extension was exactly the same thing:
www.cristianofino.net/.../...er-BlogEngineNET.aspx

I'm turning the component in a widget to adapt it to version 1.4 Wink

Blogging Developer
Blogging Developer Turkey
7/3/2008 10:02:25 AM #

Great thank you!

http://www.bloggingdeveloper.com

Nick Bastian
Nick Bastian United States
7/3/2008 1:48:32 PM #

Thanks so much for the updates. Looking forward to adding new widgets in the near future.

Amr Elsehemy
Amr Elsehemy Egypt
7/3/2008 4:04:24 PM #

This is just great, can't wait community widgets.

I think I am gonna try to build one soon.

Juan
Juan Argentina
7/4/2008 2:22:42 PM #

I created my first widget which you can download from this URL, while we wait for the community site =)

www.juanformoso.com.ar/.../...ogEngine-Widget.aspx

I love the widget framework!

Anoop
Anoop India
7/4/2008 7:02:52 PM #

This new widget framework is fantatsic. The flexibility and freedom of it is the best part. WordPress, eat your heart out.

mikedopp
mikedopp United States
7/7/2008 1:32:07 PM #

we need more themes that support this. However the widget framework is awesome so far.

Mads Kristensen
Mads Kristensen Denmark
7/7/2008 2:54:12 PM #

@mikedopp,

More themes will come very soon. We will no longer ship with more than 3-4 themes but then make sure that people can download hundreds themselves.

Gil Yoder
Gil Yoder United States
10/3/2008 5:07:30 PM #

Mads, I have just now installed BogEngine.NET on my web server, and I'm trying to understand how it all works. Somehow I got the idea that widgets were obsolete, and were replaced with the user controls found in the Standard template. I am glad I found this blog, or I wouldn't even have looked at them further.

I really appreciate how easy BlogEngine.NET is to set up and manage. And I especially appreciate your site as a resource to learn more about it.

Thanks, and if you don't mind my being trite, keep up the good work!

Jitendra Singh
Jitendra Singh India
10/7/2008 6:43:40 PM #

Hi, I am using 1.4.5, the most comments widgets is already there.
As i have created my own theme so i am pasting direct code in site.master as follows:
------------------------
<h1>Hall of fame</h1>
<ul id="asdfadsf">
<ucMostComments:MostComments  ID="ucMostComments" Title="XYZ" ShowTitle="true"  runat="Server" /></ul>
------------------------

but nothing is displayed on the main page except ... heading "Hall of fame" and title "XYZ"

what to do ???

Thanks

www.newGenLives.com

Shahid
Shahid
11/1/2008 7:15:55 AM #

Hi, I am using BlogEngine.Net 1.4 version. I love the widget framework. I will definitely develop some plug ins and will upload soon  

online dating
online dating United Kingdom
1/9/2009 5:42:16 PM #

Widget is a special control you can add to the sidebar. What makes it special is that it is a member of the "zone" - area where you can add and configure these reusable components through the zone manager. It is similar to SharePoint WebParts - you can drag it around, set properties etc. And, in BlogEngine, you can build widget just in few simple steps.

jobs for 14 year olds
jobs for 14 year olds United States
1/10/2009 9:32:20 PM #

I love the widget and its so special.

Pingbacks and trackbacks (4)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.