Glass Mapper and Umbraco 7
June 30, 2014
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:
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:
As you can see, most of these fields belong to the Properties tab on Umbraco Items:
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.
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.
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.
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.
I also created a banner item which will be used in Umbraco, representing the banner item mapped using the Multi Tree Node Picker.
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.
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.
Now all that is left to do is to have the TestPageController inherit from this base controller and pass the model to the view.
Now we can use the concrete model in the view.
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.
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.