Glass Mapper and Umbraco 7

Glass Mapper and Umbraco Image

Umbraco is an open source content management system that is great for small and big organizations and while Umbraco currently has great Razor support, most of it deals with using dynamic objects. This is great for small systems, but it can quickly clutter your view and doesn’t promote code reuse.  Umbraco also has the option of creating custom controllers to return custom models but this adds a lot of development time since you have to write the custom model mapping yourself. This is where Glass Mapper comes in to help define and map your models. For those who are new to Glass Mapper, it is a popular ORM framework that allows you to map content from a CMS to a concrete class you can code with. Glass mapper currently supports Umbraco 6 and 7, so make sure the version of Umbraco you are using is correct.

The easiest way to set up Glass Mapper with Umbraco is to use NuGet Packages. I recommend installing the Umbraco Cms NuGet package first and then installing the Glass.Mapper.Umb.CastleWindsor Nuget package.

Copy of packages and dependencies installed:

 
Packages_installed

 

Base Class:

Once you have everything setup, we can start creating our base Umbraco item. You will want all your items to inherit from this base class since it will contain useful information from Umbraco.  As you can see from the image below, we will use the “UmbracoType” class attribute to tell Glass Mapper that it will need to map this class. We will also use the “UmbracoInfo” property attribute to define properties that belong to all Umbraco items. 

Base Umbraco Item:

Base Umbraco Item

 

As you can see, most of these fields belong to the Properties tab on Umbraco Items:

Umbraco Item Properties Tab

Since all items have these fields, it is useful for all mapped classes to inherit from this base class.

 

Base _Layout file:

In order for our site to use inheritance with base classes, we want all of our pages to inherit from a base class, SitePage.cs, which will be the model for our _Layout.cshtml.

Document Type structure in Umbraco

 

This SitePage document type will contain information that all pages need to have.  I created a new tag call “Meta Content” which will contain properties that are useful for structuring the page’s meta fields.

SitePage document type properties

 

SitePages.cs

As you can see, the SitePage.cs Model is the exact copy of the Document Type in Umbraco. This will allow Glass Mapper to map the fields to the object.

Now that our SitePage.cs file has been created we can use this model in our _Layout.cshtml file, which will be used by all pages on the site.

Layout.cshtml File

This is the layout file that will be used across all pages. Since all pages will inherit from SitePage.cs, that means every page will have access to the Meta properties which will allow us to use a base layout file.

 

Your First Page:

Now that all the base files are set up, we want to create our first page. Let’s create a “TestPage” document type that inherits from the “SitePage” document type and create a model that does the same thing.

For creating a homepage with a rotating banner, the best Umbraco property type is a MultiNodeTreePicker. You will have to create this under Developer > Data Types.

Once the Data Type has been created, we can add it to our Umbraco Document Type.

Test Page document type

 

Test Page model

I also created a banner item which will be used in Umbraco, representing the banner item mapped using the Multi Tree Node Picker.

Banner Item Document Type

 

Banner Item Model

Now we want to create Banners and the Test Page in Umbraco. Once we create the Test page, we will be able to assign the banners we created to it.

Test Page

 

The Multi node picker has the option of selecting banners that we have now created.

In order for us to map the page, we need to use Custom Controllers

We created a base controller that will allow us to easily map the model from the incoming Rendering  Model.

Base Controller

Now all that is left to do is to have the TestPageController inherit from this base controller and pass the model to the view.

Test Page Controller

 

Now we can use the concrete model in the view.

Test Page Controller View 2

 

As you can see, the TestPage.cshtml using the _Layout.cshtml since the TestPage.cs model inherits the SitePage.cs model. We used Foundations orbit slider for this.

 Once we have everything up, when we visit the page, Umbraco should hit the controller and render the model. The Final result should be something like this.

* Note that we imported the foundation css and javascript on the _Layout.cshtml file.

 

Result

 

 

To learn more about Bluetube and Umbraco, please visit our open source content management with Umbraco page.

To learn more about Bluetube and our award winning mobile apps, please visit our portfolio.


Posted in: Technical | Permalink

Andres Castro
Posted by Andres Castro