My first Orchard Widget

Photo of me in the rain

When I first learnt ASP.NET I wrote a control for my website that told my visitors the weather I was currently experiencing. It was just a simple, fun project to help me learn. Now I’m using Orchard for my site I still want my visitors to know the weather I am experiencing. So this seemed like the ideal project for my first Orchard CMS module.

I wanted the module to display a widget that could be placed anywhere using Orchard’s new Widget Manager. At this point in time the documentation for making widgets isn’t complete and I’ve hit a few stumbling blocks along the way.

The first thing to realise is that a widget is just a content part with some simple extra bits, and there is a good tutorial on making content parts on the Orchard website. The simple extra bits are contained in the module’s data migration. This registers your content part as a widget when it is installed. The map module in the Modules gallery is an excellent source of code samples for this. It contains a simple Map content type which is also registered as a widget.

The next problem I hit was an unhelpful error message when I tried to install an early version of my module. The message just said ‘Some features need to be upgraded’. This was strange as this was the first time I’d tried to install it. I got some very useful advice on the discussion forum and finally found there was a problem with my data migration. I’d tried to use an unsigned integer and this was not supported by SQL Server.

A content part has a driver. This is a bit like a controller, but just for a part of the page. My widget displays the current weather based on a feed provided by BBC Backstage. The driver is the bit that queries this feed, populates a model and then passes this to view.

That’s the display part of the driver. The driver also controls editing the widget and mine allows the site administrator to change the place where the current weather is shown for.

When my module was finished and I wanted to deploy it on my real site I came across the dreaded ‘Some features need to be upgraded’ message again. It took me a while to work out that I hadn’t set all the permissions right on my live server. The IIS_IUSRS account needs write access to the App_Data folder and the data base user needs db_ddladmin rights so it can alter the data base. Mine didn’t have these because I’d moved the files and database from my test server. If I’d installed Orchard on the live server I would have realised earlier that these permissions were missing.

As I’m querying a web service and the data doesn’t change very often it makes sense to cache the response I’m getting. This is next on my list once I’ve figured out how Orchard Caching works.

You can see this widget on my homepage. I will probably leave it at this stage, but if I was to continue work on it, I would turn it into a more generic weather forecast module. Perhaps a holiday camp site would find it useful to display the forecast for their camps. I also like the idea of changing the CSS for the whole site based on the weather. I could have a sunny and cloudy site design that changed depending on the weather.

I’ve got some more module ideas for Orchard that I’m hoping to put in module gallery. Now I get the basics there’s nothing stopping me.

Christian said

Hi! It would be nice if you can attach the Widgets source code to the post to see what it contains. Regards Christian

Richard Garside said

Hi @Christian. I'll tidy it up and add it soon. In the meantime the maps module in the gallery is a really good example of a widget.

Nicholas Mayne said

That's not a London bus I see!!??

Richard Garside said

No, it's a far less iconic Leeds bus.

Jonathon Kresner said

Thanks for your post. I think we need a lot of these types of experiences documented and available for the community to kick off properly! Cheers, Jon