More in this section
Blogs RSS feed

Creating Self-installing Widgets and Modules in Sitefinity

by User Not Found


As developers, we want to simplify our life with elegant solutions that save time and solve problems. We identify steps that can be described with a process and we try to automate those.

The Motivation

The installation of Sitefinity widgets and modules is a task that can be encapsulated into a simple process which just begs to be automated. I had discussions with various Sitefinity developers and they all embraced the idea of self-installing widgets and modules. One of the many benefits is that if you have many web sites and environments, you would like to distribute all your widgets and modules into reusable dll’s and instead of manually registering each widget on every single Sitefinity instance, you can merely paste them into the bin folders and that would be it:

Automated Installation Process Diagram

The Solution

Thus, I decided to share the approach that really makes Sitefinity developers’ life easier. The approach relies on the powerful API of Sitefinity to programmatically install any widget or module.

We are also going to use the ASP.NET PreApplicationStartMethodAttribute class and you are going to be chums with it. This attribute allows you to have code that runs early in the ASP.NET chain of events. It can be triggered from external class library and it runs even before the Application_Start event.

The Instructions

We will use a PreApplicationStartMethodAttribute on a method that will hook up an event handler for the Bootstrapper.Initialized event, so that each time the Sitefinity application loads, we will have an opportunity to intercept, check to see our components are installed, and do register them if needed.

public class Installer
        /// <summary>
        /// This is the actual method that is called by ASP.NET even before application start. Sweet!
        /// </summary>
        public static void PreApplicationStart()
            // With this method we subscribe for the Sitefinity Bootstrapper_Initialized event, which is fired after initialization of the Sitefinity application
            Bootstrapper.Initialized += (new EventHandler<ExecutedEventArgs>(Installer.Bootstrapper_Initialized));

To mark this PreApplicationStart method with the PreApplicationStartMethodAttribute, we need to open the AssemblyInfo and add the attribute there. 

Solution Explorer Snapshot

The attribute needs two parameters – the type of the class which holds the method and a string with the method name. Below is the line of code, you need to paste into the AssemblyInfo.cs

[assembly: PreApplicationStartMethod(typeof(Installer), "PreApplicationStart")]

With that steps done, now you have a method that will be executed each time the application starts and you will have the power to automate any widget and module registration task. With the API of Sitefinity this is literally a few lines of code.

To help you with the latter, I am also attaching a code example that shows how you can register widgets in the toolbox, installing modules and creating virtual folders with the configuration API of Sitefinity.

By running the sample, you should be able to have the SampleWidget, the SampleModule and the Sample virtual path. (Download link here)

Sample Widget Screenshot

Try it, see how it works and last, but not least happy coding!

P.S. Please share your feedback. You can also follow me on Twitter @PeterTenev


Leave a comment
  1. Mark Mar 20, 2013
    Thank you for posting this, it's going to save me a ton of time by not having to register every widget I add to my assembly on 20+ sites!

    I'd like to add a check to see if a ToolboxSection exists by name and if it doesn't create it. I have my check to see if it exists but I'm not sure how to create it if it it doesnt,

    Something like: 
    ToolboxSection toolboxSection = toolboxSections.Where<ToolboxSection>((ToolboxSection x) => x.Name == toolboxName).FirstOrDefault<ToolboxSection>();
    if (toolboxSection == null)
    //create it
  2. Peter (Sitefinity Team) Mar 21, 2013
    Hi Mark,

    You don't have to worry about that detail. The API does that for you. Actually, the LoadOrAddWidget<TWidget>(string widgetName) facade* I have used in the sample does that automatically for you. Only if the toolbox item does not exist, the method creates a new toolbox item.

    * that facade is part of the fluent API that comes with Sitefinity. To learn more about our implementation, please read our documentation (link here)
  3. Darrin Mar 21, 2013
    This is great. I will start using this.
     Do you know how to register controls through Viewmap's as well?
  4. Pavel Donchev Mar 22, 2013
    Hello, Peter!
    Great blog post!
    You are right for the most of the things. We are using this mechanism to install the Copy.Paste.Done for Sitefinity since we made it back in November. There were some techincal issues back then because of Siteifnity wasn't ready for such an installation.

    Some clarifications need to be made. The most important one is that we think the suggested practice won't work on Sitefinity versions prior the support for MVC was introduced. Read more technical details why do we believe so in the blog post we just made to complement yours:  How the Copy.Paste.Done installer works?
  5. Peter (Sitefinity Team) Mar 23, 2013
    @ Darrin - Thank you. Yes, you can just use the native API:
                // get the configManager
                var configManager = Config.GetManager();
                // get the collection of ViewMap items to add/edit/delete them
                var viewMap = configManager.GetSection<ControlsConfig>().ViewMap;

    @Pavel Thank you for the feedback. An important clarification here is that this method WORKS with all Sitefinity 4.x versions even before the support added for MVC. Let me explain:

    The PreApplicationStartMethodAttribute class is member of System.Web and it is part of .NET 4.0 and surely .NET 4.5. This means that any .NET 4.0 WebForms web application will be able to utilize that method and Sitefinity happens to be one of those. Personally, I have successively applied this approach with Sitefinity 4.4, but there are others who have been using it since the early days of Sitefinity 4.0

    As for your errors, we must search for reasons other than MVC dependencies. Please feel free to reach me and we can explore them together. Thanks again!
  6. Pavel Donchev Mar 28, 2013
    You're right, Peter!
    I took a look at the code and the reference to Microsoft.Web.Infrastructure is needed to dynamically register an HttpModule at runtime.

    Post updated.
  7. Rick van Zeeland Apr 10, 2013
    Hello Peter,

    Thanks for this guide! Unfortunately I can't get 1 thing to work (probably due to something I'm missing).

    The widget installs like it should and I am able to drag the widget onto my page and fill in custom values when using the designer. But as soon as I go to the frontend I get a pretty "object reference" error.

    While debugging I found that the code crashes as soon as it hits the repeater which binds the images. So my guess is that it can't find the .ascx that is associated with the widget. I've set the build action on "Embedded resource" like in your example and I've pretty much followed the virtualpath setting that you had in your example (with my own project name).

    Do you have any idea what I could be missing? It's probably hard to find without looking at the project but maybe it's something obvious :)

    Rick van Zeeland

    Leave a comment