Create Orchard Module in a Separate Project

The Orchard Project is a new OOS Microsoft project that is being developed up on CodePlex. From the Orchard home page on CodePlex, it states

“Orchard project is focused on delivering a .NET-based CMS application that will allow users to rapidly create content-driven Websites, and an extensibility framework that will allow developers and customizers to provide additional functionality through modules and themes.”

The Orchard Project site contains additional information including documentation and walkthroughs. The ability to create a composite solution based on a collection of modules is a compelling feature. In Orchard, these modules can just be created as simple MVC Areas or they can also be created inside of stand-alone web application projects.  The walkthrough for writing an Orchard module that is available on the Orchard site uses a simple Area that is created inside of the host application. It is based on the Orchard MIX presentation. This walkthrough does an effective job introducing various Orchard concepts such as hooking into the navigation system, theme/layout system, content types, and more.  However, creating an Orchard module in a separate project does not seem to be concisely documented anywhere. Orchard ships with several module OOTB that are in separate assemblies – but again, it’s not well documented how to get started building one from scratch.

The following are the steps I took to successfully get an Orchard module in a separate project up and running.

Step 1 – Download the file from the Orchard Release page. Unzip and open up the solution.

Step 2 – Add your project to the solution. I named my project “Orchard.Widget” and used and “MVC 2 Empty Web Application” project type. Make sure you put the physical path inside the “Modules” sub-folder to the main project like this:

At this point the solution should look like:

Step 3 – Add assembly references to Orchard.dll and Orchard.Core.dll.

Step 4 – Add a controller and view.  I’ll just create a Hello World controller and view. Notice I created the view as a partial view (*.ascx). Also add the [Themed] attribute to the top of the HomeController class just like the normal Orchard walk through shows it.

Step 5 – Add Module.txt to the project root. The is a very important step. Orchard will not recognize your module without this text file present.  It can contain just the name of your module: name: Widget

Step 6 – Add Routes.cs. Notice I’ve given an area name of “Orchard.Widget” on lines 26 and 33.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Web.Mvc;
   4:  using System.Web.Routing;
   5:  using Orchard.Mvc.Routes;
   7:  namespace Orchard.Widget
   8:  {
   9:      public class Routes : IRouteProvider
  10:      {
  11:          public void GetRoutes(ICollection<RouteDescriptor> routes)
  12:          {
  13:              foreach (var routeDescriptor in GetRoutes())
  14:              {
  15:                  routes.Add(routeDescriptor);
  16:              }
  17:          }
  19:          public IEnumerable<RouteDescriptor> GetRoutes()
  20:          {
  21:              return new[] {
  22:                  new RouteDescriptor { 
  23:                     Route = new Route(
  24:                         "Widget/{controller}/{action}/{id}",
  25:                         new RouteValueDictionary {
  26:                             {"area", "Orchard.Widget"},
  27:                             {"controller", "Home"},
  28:                             {"action", "Index"},
  29:                             {"id", ""}
  30:                         },
  31:                           new RouteValueDictionary(),
  32:                           new RouteValueDictionary {
  33:                               {"area", "Orchard.Widget"}
  34:                           },
  35:                           new MvcRouteHandler())
  36:                  }
  37:              };
  38:          }
  39:      }
  40:  }

Step 7 – Add MainMenu.cs. This will make sure that an item appears in the main menu called “Widget” which points to the module.

   1:  using System;
   2:  using Orchard.UI.Navigation;
   4:  namespace Orchard.Widget
   5:  {
   6:      public class MainMenu : INavigationProvider
   7:      {
   8:          public void GetNavigation(NavigationBuilder builder)
   9:          {
  10:              builder.Add(menu => menu.Add("Widget", item => item.Action("Index", "Home", new
  11:              {
  12:                  area = "Orchard.Widget"
  13:              })));
  14:          }
  16:          public string MenuName
  17:          {
  18:              get { return "main"; }
  19:          }
  20:      }
  21:  }

Step 8 – Clean up web.config. By default Visual Studio adds numerous sections to the web.config. The sections that can be removed are: appSettings, connectionStrings, authentication, membership, profile, and roleManager.

Step 9 – Delete Global.asax. This project will ultimately be running from inside the Orchard host so this “sub-site” should not have its own Global.asax.


Now you’re ready the run the app.  When you first run it, the “Widget” menu item will appear in the main menu because of the MainMenu.cs file we added:

We can then click the “Widget” link in the main menu to send us over to our view:



From start to finish, it’s a relatively painless experience but it could be better. For example, a Visual Studio project template that encapsulates aspects from this blog post would definitely make it a lot easier to get up and running with creating an Orchard module.  Another aspect I found interesting is that if you read the first paragraph of the walkthrough, it says,

“You can also develop modules as separate projects, to be packaged and shared with other users of Orchard CMS (the packaging story is still to be defined, along with marketplaces for sharing modules).”

In particular, I will be extremely curious to see how the “packaging story” evolves. The first thing that comes to mind for me is: what if we explored MvcContrib Portable Areas as a potential mechanism for this packaging? This would certainly make things easy since all artifacts (aspx, aspx, images, css, javascript) are all wrapped up into a single assembly. Granted, Orchard does have its own infrastructure for layouts and themes but it seems like integrating portable areas into this pipeline would not be a difficult undertaking. Maybe that’ll be the next research task. :)

posted on Thursday, April 22, 2010 6:34 PM Print
# re: Create Orchard Module in a Separate Project
Michael Cullina
4/27/2010 12:50 AM
Nice job. I walked through it and it worked perfectly. Helpful. By the way, I enjoyed your presentation recently in Philly.
# re: Create Orchard Module in a Separate Project
Ian Grainger
6/30/2010 12:56 PM
Anyone tried to create a module with the current latest dev code?

I followed the 'Hello World' tutorial with that code and just get 'The resource cannot be found'.
# re: Create Orchard Module in a Separate Project
Ian Grainger
7/1/2010 7:43 AM
OK, it looks like this doesn't work because the only thing which allows you to create a new module as instructed in the walkthrough is the RELEASED Web.PI project (from back in March).

Using the Orchard.Web project from the head of the default branch or the dev branch will not add the item to the main menu and not find the controller :(

It's a shame there's not an updated sample module in the source repository I could use instead of the (apparently out-of-date) walkthrough.
# re: Create Orchard Module in a Separate Project
3/19/2013 6:43 AM
Good Job. I tried this and created a new module.

Post Comment

Title *
Name *
Comment *  

View Steve Michelotti's profile on LinkedIn

profile for Steve Michelotti at Stack Overflow, Q&A for professional and enthusiast programmers

Google My Blog

Tag Cloud