Friday, September 12, 2014

Creating A Sitecore Page With ORM Tool ( Glass Mapper)

Glass Mapper is an object mapping framework (ORM tool) for mapping Sitecore items directly onto an object model allowing you to model your entire Sitecore solution as a data model. The framework handles converting data to and from fields and pulling data from related items. It also helps in making Sitecore code unit testable.

Below example uses CastleWindsor dependency injection framework.


Step 1: Install Glass.Mapper.Sc.CastleWindsor from NuGet packages


Step 2: Create two base templates “Page Title” and “Page Description” 











Step 3: Create a page inheriting from two templates














Step 4: Insert standard values by selecting “Builder” tab of data template and clicking on “Options” in the main menu.

















Step 5: Create a layout and put necessary place holders.



















In DemoLayout.aspx

<div id="MainPanel">

      <sc:placeholder key="main" runat="server" />

   </div>

Placeholder is an area of the page to support the user adding controls. Placeholders can be placed on layouts, sublayouts and view renderings.

Step 6: Create a sub layout


Step 7: Create a Model based on the data template

using System;
using System.Web;
using Glass.Mapper.Sc.Configuration.Attributes;

namespace Glass.Mapper.Models.Demo
{

    public class DemoPage
    {

        [SitecoreField("Page Title")]
        public virtual string PageTitle { get; set; }
        [SitecoreField("Page Description")]
        public virtual string PageDescription { get; set; }

    }

}

Step 8: Load model of the current item using SitecoreContext

using Glass.Mapper.Sc;
using Glass.Mapper.Models.Demo;

public partial class layouts_PageData : System.Web.UI.UserControl
{

    public DemoPage Model { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {

        var context = new SitecoreContext();
        Model = context.GetCurrentItem<DemoPage>();

    }

}

Step 9: Render data from model on to the ascx page.

<h1><%= Model.PageTitle%></h1><br />
<%= Model.PageDescription %>

Step 10: Create a place holder setting item and allow above sublayout. Placeholder settings provide all of the details for the placeholder to Page Editor including the controls that can be added.



















Step 11: Assign layout, sub layout and placeholder settings to the template.

To set layout, go to standard values, click on edit and set the layout













Click on controls and set the sub layout


























Set the placeholder setting



























Step 12: Set Insert Options for the home item template to “Page” template.


















Create a page based on the template.



1 comment: