Creating widgets for BlogEngine.NET 1.4

by Mads Kristensen 3. July 2008 03:01

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

Tags: ,

BlogEngine

Comments

7/3/2008 3:39:27 AM #

Juan

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

Juan Argentina |

7/3/2008 5:22:06 AM #

Domenic

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.

Domenic United States |

7/3/2008 6:34:41 AM #

Troy Goode

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.

Troy Goode United States |

7/3/2008 7:19:17 AM #

trackback

Trackback from DotNetKicks.com

Creating widgets for BlogEngine.NET 1.4

DotNetKicks.com |

7/3/2008 9:10:23 AM #

Josh Stodola

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!

Josh Stodola United States |

7/3/2008 11:36:05 AM #

Justin Etheredge

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

Justin Etheredge United States |

7/3/2008 2:52:23 PM #

Mads Kristensen

@Troy,

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

Mads Kristensen United States |

7/3/2008 4:06:29 PM #

Cristiano

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

Cristiano Italy |

7/3/2008 7:02:25 PM #

Blogging Developer

Great thank you!

http://www.bloggingdeveloper.com

Blogging Developer Turkey |

7/3/2008 10:48:32 PM #

Nick Bastian

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

Nick Bastian United States |

7/4/2008 1:04:24 AM #

Amr Elsehemy

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

I think I am gonna try to build one soon.

Amr Elsehemy Egypt |

7/4/2008 11:22:42 PM #

Juan

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!

Juan Argentina |

7/5/2008 4:02:52 AM #

Anoop

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

Anoop India |

7/7/2008 10:32:07 PM #

mikedopp

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

mikedopp United States |

7/7/2008 11:54:12 PM #

Mads Kristensen

@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.

Mads Kristensen Denmark |

7/8/2008 12:35:46 PM #

trackback

Trackback from cnblogs.com

Always 英文技术文章参照( 四 ){ UpdateTime:2008-7-8; } My article in the cnblogs

cnblogs.com |

8/14/2008 6:53:52 AM #

trackback

Building a Photo Album widget for BlogEngine.NET

Building a Photo Album widget for BlogEngine.NET

Rickard Nilsson |

10/4/2008 2:07:30 AM #

Gil Yoder

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!

Gil Yoder United States |

10/8/2008 3:43:40 AM #

Jitendra Singh

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

Jitendra Singh India |

11/1/2008 4:15:55 PM #

Shahid

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  

Shahid |

1/10/2009 2:42:16 AM #

online dating

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.

online dating United Kingdom |

1/11/2009 6:32:20 AM #

jobs for 14 year olds

I love the widget and its so special.

jobs for 14 year olds United States |

9/28/2009 9:50:34 PM #

trackback

BlogEngine.NET Xbox Gamercard Widget

BlogEngine.NET Xbox Gamercard Widget

Lots of Ideas |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008