Writing an Orchard Webshop Module from scratch - Part 3

updated to be compatible for Orchard >= 1.4.

This is part 3 of a tutorial on writing a new Orchard module from scratch.
For an overview of the tutorial, please see the introduction.

In Orchard, a module is really just an ASP.NET MVC Area class library that follows both ASP.NET MVC as Orchard specific conventions.
Amost all of the features that come out of the box are implemented as modules. The only exception is the Orchard.Framework class library and the web site itself. The Framework contains all that is needed to host Modules, and the web site uses this framework to fire up a shell that will load all of the installed modules and themes.

To create a module, the minimum requirements are that you create a class library and add a text file to it that is called Module.txt, which in turn has a minimum amount of information to describe the module: one of which being the name of the module.
We'll have a look at the manifest in a few moments.

Instead of creating a module by hand, we could use the Orchard command line application to generate a basic structure for us. This will create a class library with some tweaked settings, project references to Orchard.Core and Orchard.Framework, a stub manifest and a default set of folders and files. However, I think it's interesting to do all of this ourselves to make sure that no special magic is required to create an Orchard module, so that's what we're gonna do next.

Although our module will not look like much when we first create it, but in the end, our module will consist of:

  • A Module Manifest
  • Models & ViewModels
  • Views
  • Controllers
  • Drivers
  • Service classes
  • Handlers
  • Shapes
  • Javascript
  • Css
  • A Migration
  • A ResourceManifest
  • Routes
  • Helpers methods

We will discuss each item as we go along.

Let's get started, shall we?

Creating the Class Library

First, let's create a new Class Library project.

Inside Visual Studio, create a new Class Library Project under the Modules Solution Folder, and make sure that the project is physically located in \src\Orchard.Web\Modules:

Creating a new class library project for an Orchard module

Orchard requires that your module exists physically in the Modules directory.
First, let's get rid of the Class1.cs file that Visual Studio created for us, since we have no use for it (we will create our own classes later).
Next, we will create the Module Manifest file, which is a simple text file describing our module, with things such as the name of our module, a description, version number, our company url, etc.

Let's start with entering the following information to Module.txt:

Name: Skywalker.WebShop
AntiForgery: enabled
Author: Sipke Schoorstra
Website: http://skywalkersoftwaredevelopment.net
Version: 1.0
OrchardVersion: 1.4
Description: Orchard Webshop Module Demo
Category: Webshop

Orchard Webshop Module Manifest

And there you have it: a basic Orchard module that does precisely: nothing. But that's about to change.

Before we contrinue, let's make sure that Orchard recognizes our module. Launch the website by pressing CTRL + F5.
Once the site starts, navigate to the admin area.

To enter the admin area, either navigate to http://localhost:30320/OrchardLocal/admin, or click on the "Dashboard" link in the site footer.
Once inside the admin, go to Modules and enable our Orchard Webshop Module:

Enabling a module

Module is enabled

And that's all it takes to create a bare bones module! Next, let's make it do something useful.
What we want is to enable the administrator to define all sorts of products: Books, CDs, DVDs, Phones, etc.
All these products have at least two properties in common: a unit price and a SKU.

At the heart of Orchard is the fact that every piece of content consists of content parts.
For example, the administrator will be able to define a new type of content: Book. The Book content type is then defined by its Content Parts.
The administrator can pick the content parts he likes, like BodyPart, CommentsPart, RoutePart, etc.

In order to turn any content type into a product that has a unit price and a sku, we need to introduce our own custom part: a ProductPart.

Part 4 - Defining the ProductPart


Source code: Part03.zip.


  • ... Posted by Wangzhongyu11 Posted 03/25/2012 03:08 AM
    <p>this dose't work .<br> I use orchard 1.4 </p>
  • ... Posted by Rnt Posted 04/04/2012 08:33 AM

    Wangzhongyu11: Check that you have webshop module in /src/modules/orchard.webshop not in /src/orchard.webshop

  • ... Posted by William Patrick Posted 05/24/2012 09:16 AM [http://www.tatvasoft.com/]

    I thought that with Orchard this won't be that easier but looking at the solution over here I felt that this is far way easier to what I thought off. Writing some codes can unleash the power of Orchard.

  • ... Posted by Nash Boyd Posted 12/17/2012 05:51 AM [http://www.amazines.com/article_detail.cfm/5223770?articleid=5223770]

    It's also essential to find out the business model of a business you would like to employ the service of.

  • ... Posted by Kennedy Knapp Posted 12/28/2012 09:01 AM [http://hamoljecal.webs.com/]

    You provide here good information which is very useful to the developer as well as beginners even they got an idea and inspiration for their application.

  • ... Posted by Herman van der Blom Posted 10/05/2016 12:45 PM

    Wow, a lot of work went in this... I need a baseline for a CMS. Thinking of Orchard. I like Entity Framework but may be Hibernate is also good to go with. Still need some research if Orchard is te best to work with. First fail with Orchard I already have. I need a Bootstrap template. Loaded one in Orchard and got several errors. Not a very convincing start...

  • ... Posted by Sipke Schoorstra Posted 10/06/2016 08:49 AM (Author)

    Hi Herman, when working with Orchard and data access, you typically don't have to deal with the ORM directly. If you;re working with content items, you work with higher-level abstractions such as IContentManager. If you want to deal with entities directly, you work with IRepository<T>. If you need to implement more advanced scenarios, then you deal with HQL. I like EF also, but found that NHibernate works pretty good, and was chosen back in the days due to it being much more advanced and mature (Orchard 2 on the other hand won't be using NHibernate, but implements a truly pluggable data access story, and uses YesSQL by default).

    Sorry to hear about your first fail. Unfortunately, Orchard does not control third party themes.

Leave a comment