+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

Creating Intra-Site Modules in Sitefinity 4

by Josh Morales

In our last post, we looked at how to use the Sitefinity Backend Page Editor to easily create Page Based Modules. This time we'll take things a step further by wrapping this process into a Sitefinity module, complete with support for installing and uninstalling with the click of a button!

Video

In the video below, I walk you through converting the Page-Based module into a self-contained, installable module, similar to the "Intra-Site" Modules of Sitefinity 3.x. We reuse the same User Controls from the previous video on Page-Based Modules, registering them in both the Admin pages the Installer creates as well as adding them to the toolbox.

Click below to watch the video, then read on for more details.

video-thumbnail

Watch on Telerik.tv

ModuleBase

To define a custom module in Sitefinity 4, you simply need to inherit from ModuleBase. Although there is a more specialized base class we would use for Content-based Modules (ContentModuleBase), in this case, we are not using any Sitefinity components, but rather have developed our own controls that we simply want to open inside of Sitefinity.

Each of the abstract methods needs to be implemented, however once again, since our module is simple, we can return null or simply ignore most of them. The main components of Intra-Site Modules are the LandingPageId property and the Install method.

LandingPageID

This is simply used to hard-code a unique identifier for the admin page for your module. This can be any Guid value, but each module of course must have its own unique value.

In addition, you can also add additional page IDs if you want your module to install multiple pages or subpages. This is handled in the Install method.

Install

Here is where everything that is needed to run the module is setup, including creating the pages, registering the admin controls inside that page, and adding the widget to the toolbox.

The completed install routine is available within the downloads section below, which contains module we created in the video, fully commented and ready to add to any existing Sitefinity website.

Enhanced Installation and Uninstall Methods

Once the module is completely defined, there is nothing more that needs to be done other than install it in the Sitefinity Administration, as outlined in the documentation: Registering a Module.

However, by adding a pair of static methods to the class that do this same process in code, we can then create an installer page. This installer page calls these static methods as needed to install or uninstall the module, which takes care of registering (or removing) everything for you.

The methods we created for this module even take care of restarting Sitefinity so that you do not have to open and save your web.config. This allows complete control of the module within Sitefinity.

What's Next

Implementing the Install and Uninstall methods open up a great extensibility point in Sitefinity, allowing you to reuse a lot of existing controls and easily import them into Sitefinity. Best of all, since they are compiled along with your project, there are minimal issues with compatibility between Sitefinity releases.

There are several ways we can improve this, including moving everything to a separate class library so that you can distribute your module in a truly self-contained manner, perhaps through the Sitefinity Marketplace.

I will also be exploring how you can further enhance intra-site modules by tapping into security, workflow, and other integrated features of Sitefinity. Stay tuned for more.

In the meantime, download the sample project below and give it a try. I also plan to develop a more "real world" example for the next release of the SDK coming in a few weeks. If you have any suggestions for the type of module you'd like to see implemented, please leave your comments below, or as always, visit our Sitefinity Discussion Forum.

Downloads

Instructions: Extract to the root of your site, so that the path is ~/Modules/HelloWorld. Then launch the page ~/Modules/HelloWorld/Setup.aspx.

11 comments

Leave a comment
  1. Josh May 26, 2011
    One thing this module does not do is handle creation of the database, because we did that in the previous video. 

    A future example will use OpenAccess to also automate setting up the database and mapping. Hopefully this will be included in the SDK example
  2. Magnus Lindeberg May 27, 2011
    Best video tutorial from sitefinty!

    Thank you Josh!!
  3. Josh Jun 24, 2011
    is your site a Web Site Project or a Web Application Project? To be copiled correctly you need to be using a web application project, or move the class definitions to the App_Code folder.  
  4. Amir Aug 12, 2011
    @Josh... at 20:38, you mentioned SiteMapBase.FlushCache();.  This method seems to be obsolete in 4.2.  I did not see a reference to this in the "Breaking Changes" documentation.  What is the appropriate replacement for this method?  I see the "FlushNodeFiltersCacheForAllUsers". Is that it?

    Thank you.
  5. Bob Aug 23, 2011

    @Amir

    Sorry, it is our omission not including this in breaking changes. We removed that method as it encourages very bad practice. Since SiteMap nodes are dependent on so many things and flushing the entire cache too often makes it very inefficient. In most cases you should not need to take care of this as sitefinity places appropriate cache dependencies for every cached node. This way only affected nodes will expire. The appropriate way of adding additional logic for node expiration is with custom cache dependencies.  Unfortunately, working with custom cache dependencies is not documented yet.

    In case you really need to flush the entire sitemap cache you can do it like this:
       
    SystemManager.GetCacheManager(CacheManagerInstance.SiteMap).Flush();
     

    I hope this helps.

  6. Dan Dec 22, 2011
    Hi

    I added the code from the download to root of site, think all correct but i get;

    Parser Error Message: Could not load type 'SitefinityWebApp.Modules.HelloWorld.Setup'.

    when i launch the page.
  7. Josh Dec 28, 2011
    Dan, make sure that the type that you specify when you add the module to Sitefinity is the actual type of the module. In this case the module type is SitefinityWebApp.Modules.HelloWorld.HelloWorldModule.

    Let me know if that doesn't work, thanks for your feedback!
  8. saroj rout Jan 11, 2012
    This is for testing
  9. Med Mar 27, 2012
    Hi and thankx for the tuto,
    Can you tell us please how can we make a widget that get images (binary type) from a database, using axd !
    thank you 
  10. Rico Mar 14, 2013
    This doesn't seem to work if you access the 'backend' site (admin site) as a user that is not designated the 'administrator' role. I was trying to create a backend module for a specific role of user. However, by following this demo (don't get me wrong, it's great and it works), but it only works for 'administrators'. When I log into the site as a backend user who is not an administrator, the 'Hellow world' module is simply a blank page (the user control doesn't render). Can you please help with this? I'm also currently using v5.2 (not 4)...

    Thanks!
  11. Rico Mar 14, 2013
    This doesn't seem to work if you access the 'backend' site (admin site) as a user that is not designated the 'administrator' role. I was trying to create a backend module for a specific role of user. However, by following this demo (don't get me wrong, it's great and it works), but it only works for 'administrators'. When I log into the site as a backend user who is not an administrator, the 'Hellow world' module is simply a blank page (the user control doesn't render). Can you please help with this? I'm also currently using v5.2 (not 4)...

    Thanks!

    Leave a comment