How to Get Started With Orchard to Create a Module

2014/08/012 min read
bookmark this
Responsive image

Table of Contents

  1. Introduction
  2. Download and Set Up Orchard
  3. Create the Scaffold Project
  4. Enable Permissions
  5. Conclusion

Introduction

This blog shows how to create a HelloWorld module in Orchard, an ASP.NET MVC open source application, and how to add the module as a role at the create role page.

Download and Set Up Orchard

First, you need to download Orchard. The following is a great blog to get you started — it has more detailed information on how to create a HelloWorld module than this blog:

HelloWorld Orchard

After you download the source code, you can just build and start the web application as Orchard.Web, and then you can start your web application.

Create the Scaffold Project

Based on the above Orchard Hello document, you can create the scaffold project by typing the following command:

Orchard.Web\bin>orchard.exe
orchard> codegen module HelloWorld
orchard> feature enable HelloWorld

If you add a controller and view like the following, then your first HelloWorld is finished.

Enable Permissions

To enable permissions, you need to create a Permissions file that implements IPermissionProvider. The Orchard framework will inject that IPermissionProvider to generate the actual logic.

using Orchard.Environment.Extensions.Models;
using Orchard.Security.Permissions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HelloWorld
{
    public class Permissions : IPermissionProvider
    {
        public static readonly Permission SetHomePage = new Permission { Description = "Set Accounting Page", Name = "SetHomePage" };

        public virtual Feature Feature { get; set; }

        public IEnumerable GetPermissions()
        {
            return new[] {
                SetHomePage
            };
        }

        public IEnumerable GetDefaultStereotypes()
        {
            return new[] {
                new PermissionStereotype {
                    Name = "Administrator",
                    Permissions = new[] {SetHomePage}
                },
                new PermissionStereotype {
                    Name = "Editor",
                    Permissions = new[] {SetHomePage}
                }
            };
        }

    }
}

You also need to add the new module to the database.

After everything is finished, you will notice that in the Add Role view, your HelloWorld module has been added.

Conclusion

Creating a module in Orchard is straightforward: scaffold the project, add a controller and view, and implement IPermissionProvider for permissions. Simple, isn't it!?