Sitecore DevOp Series – Part 4 – Setup Sitecore Glass

This is the part 4 of the Sitecore DevOp series. Previously, we have installed a local instance of Sitecore, created it’s VS project solution and added TDS projects. In this blog post, we will configure Sitecore Glass Mapper for automated code generation using T4 templates. The blog series is aimed at newer audience and developers who are setting up CI for the first time.

Sitecore Glass

Sitecore Glass is an Object-Relation-Mappper (ORM) for Sitecore items. A common ORM that you may have worked previously would be Linq to SQL. Sitecore Glass helps in referencing Sitecore data item properties by using strongly typed C# objects. It is an open-source project and more information and tutorials are available from their official website.

Step 1 : Install Sitecore Glass for domain project

Sitecore Glass Mapper is available as NuGet package from the official Nuget feed server.
Assuming you have setup the ‘MyProject.Domain’ as per the previous post of the blog series, add a reference to the ‘Sitecore.Kernel.dll‘ within ‘MyProject.Domain‘ project:

my-project-glass-ref

Then add the official package via NuGet:

my-project-glass-ref-nuget

Remove the ‘App_Config‘ and ‘App_Start‘ folders from the ‘MyProject.Domain‘ project and add a custom class file as ‘Models.Generated.cs

my-project-glass-ref-remove-folders

Step 2 : Install NuGet package for main project

Install the same NuGet package for the ‘MyProject‘ , which is your main MVC project.

my-project-glass-ref-main

As of now, upon successful installation, you should see references to Glass.Mapper, Glass.Mapper.Sc and Glass.Mapper.Sc.Mvc. 

Step 3 : T4 Templates for Auto-Code Generation

As a starter, download the T4 templates as provided by the HedghogDevelopment github repository .

  1. Navigate to ‘TDS.Master’ project properties > code generation tab and check the box for ‘Enable Code Generation’. A folder will appear in the VS tree view as ‘Code Generation Templates’.
  2. Copy the downloaded T4 Templates files (with extensions .tt) that you downloaded from github within that folder and include them in the project via VS.
  3. Set the Target Project as ‘MyProject.Domain
  4. Set the code generated file as ‘Generated.Models.cs
  5. Set the base name as ‘Models
  6. Set the header transform file as glassv3header.tt
  7. Set the base project transform file as glassv3item.tt

 

my-project-t4-2

When you hit save, the Models.Generated.cs should give you auto-generated code for the ‘Sample Item‘ template as below:

my-project-t4-3

This is pretty amazing if you are doing it for the first time as now you can auto-generate your Sitecore data template properties and reference them in your main project as strongly typed objects. Every time you make a change to Sitecore templates, all you have to do is right-click the TDS.Master project, do a sync with Sitecore and it will update the code base. You can also ‘Re-Generate code for all items‘ as shown below:

my-project-t4-4
Adding reference to Glass mapper and configuring TDS project with code generation will make sure that any field name changes that you make in data templates are reflected back in the code. If there are any compilation issues due to referencing make sure you .NET version is correct.

We are all done in setting up the project locally, the next step is to add our project to a choice of our source control so it can be shared with rest of the team and with our build server. This will be explained in the next blog post.

Stay tuned.

Thanks

Related Blogs

  1. Part 1 – Continuous Integration – Why your Sitecore project deployments must be automated ?
  2. Part 2 – Setup and Configure Visual Studio Sitecore Project
  3. Part 3 – Setup and Configure TDS
  4. Part 4 – Setup Sitecore Glass
  5. Part 5 – Setup Source Control (Git)
  6. Part 6 – Setup QA Server, DB server and CI server
  7. Part 7 – Setup Continuous Integration using Team City
  8. Part 8 – Setup Slack Notifications with TeamCity and Bitbucket
Advertisements

Sitecore DevOp Series – Part 3 – Setup and Configure TDS

In previous blogs of the DevOp series, we have installed a local instance Sitecore and configured our VS for the sample Sitecore project. In this blog, we will add Team Development for Sitecore (TDS ) projects to our VS project. The blog series is aimed at newer audience and developers who are setting up CI for the first time.

Why do I need TDS ?

TDS is a great product for developing Sitecore projects. If you have not used it before, I would strongly recommend using it for your existing and new projects. The TDS seamlessly integrates with the Visual Studios and gives you a lot of options to manage Sitecore items. One of the main feature of TDS is that you can store Sitecore Items as serialized files on your local machine and push them to the source control repository. The development team can get the latest from source control, sync changes using TDS commands and update their local instance of Sitecore databases. This feature alone will save you and your team hundreds of hours development time.

The TDS can also automatically package up the data template changes for installations and deployments. It also gives you many more advance features for continuous integration which you can read on their official site.

Setup TDS Projects

Technically, you can have just one TDS project for your website and also configure it for continuous deployment. However, inspired from Pavel’s blog and personal experiences,  in this series we will be setting up the following 4 TDS projects

  1. MyProject.TDS.Core
  2. MyProject.TDS.Master
  3. MyProject.TDS.Master.Content
  4. MyProject.TDS.Master.System

TDS.Core project will be used to store any Sitecore’s Core database items like custom profile item or a new field type information.

TDS.Master project will be used to store Sitecore’s Master database items that are more specific to developers like templates, renderings, layouts, placeholder settings.

TDS.Master.Content project will be used to store Sitecore’s Master database items under the ‘Home’ node. If you have multiple sites, you can have multiple TDS.Master.Content projects, one for each site.

TDS.Master.System project will be used to store Sitecore’s Master database items under ‘System’ node. The ‘System’ node have items like ‘Workflow‘ or ‘Publishing Targets‘ or ‘Sitecore Powershell Scripts‘ that should be part of the source control, but we don’t necessarily need to deploy them every time.

All of the above TDS projects are optional and are meant for simplifying the deployment process as you will see in coming posts of the blog series.

Step 1 : Create TDS.Core project

Assuming that you have installed TDS for the correct version of your Visual Studios, right-click on the VS solution and click on ‘Add New Project’ and the select ‘TDS Project with Wizard’.

Give the project appropriate name like ‘MyProject.TDS.Core‘ and click on ‘Next’

my-project-tds-core1

On the next screen, set up the following parameters :

  • Sitecore Web Url
  • Sitecore Deploy Folder
  • Source Web Project
  • Sitecore Database
  • Check the install Sitecore connector

The example configuration is shown below:

 

my-project-tds-core2

Once you click Ok, a success message will pop-up and the new project should be added as below:

my-project-tds-core3

Step 2 : Create TDS.Master project

This step is similar to the previous one, except there is a small change when adding parameters on screen 2 of the wizard.

Right-click on the solution and click on ‘Add New Project’ and select ‘TDS Project with Wizard’. Give it the name as ‘MyProject.TDS.Master’ as shown below:

my-project-tds-master1

On the second screen, make sure to change the ‘Sitecore Database’ option to ‘master‘ and also un-tick the ‘Install Sitecore Connector’.

my-project-tds-master2

Click Ok and the project should be add to the VS solution:

my-project-tds-master3

Step 3 : Add TDS.Master.Content Project

Repeat the configurations for step 2, only changing name of the project

MyProject.TDS.Master.Content

my-project-tds-master-content1

Same parameters as that of step 2, un-tick the ‘Install Sitecore Connector’ check box:

my-project-tds-master-content2

New project should be added on clicking Ok

my-project-tds-master-content3

Step 4 : Create TDS.Master.System Project

Repeat the configurations for the step 2, only change the name of the project

MyProject.TDS.Master.System

my-project-tds-master-system1

Same parameters as that of step 2, un-tick the ‘Install Sitecore Connector’ check box:

my-project-tds-master-system2

New project should be added on clicking Ok.

my-project-tds-master-system3

Step 5 : Get Sitecore Items

So now we have our TDS projects installed and we would like to populate these propjects with Sitecore items. When you will right-click on the ‘MyProject.TDS.Master‘ project and try to click ‘Get Sitecore Item‘ command, it will be disabled as shown below:

my-project-tds-master-get-content1

This is intended behavior as during setup of the TDS.Master project we left the ‘Install Sitecore connector‘ option un-ticked. If you have left it ticked during the project wizard, it will create a different Access GUID for each project which will be incorrect for our 4 TDS project setup.

Copy Sitecore Access GUID

As we have only installed the connector for TDS.Core project, we need to get the ‘Access Guid’ and copy it across to the TDS.Master Project

For this, right-click on the TDS.Core project’s properties and navigate to the ‘Build’ tab. At the end of the panel, you will find ‘Sitecore Access GUID‘ property. Select and copy that value

my-project-tds-master-get-content2

Paste Sitecore Access GUID

Navigate to the build tab of ‘TDS.Master‘ project’s properties build tab and now tick the ‘Install Sitecore Connector‘ box, a random GUID will appear. Delete that GUID and paste the same GUID as for the TDS.Core project as shown below:

my-project-tds-master-get-content3

Verify connection for TDS.Master

Once you have copied over the same GUID, click on the ‘Test‘ button at the end of the panel to verify the connection details. They should all be green as shown below:

my-project-tds-master-get-content4

Get Sitecore Items

Once you have ran the test, give it like 60 seconds and try to ‘Get Sitecore Items’ again, now all options should be available and you should be be to get Sitecore items as shown below:

my-project-tds-master-get-content5

If you select to add the standard ‘Sample Item’ template and all its children by right-click options, it should appear as below:

my-project-tds-master-get-content6

Repeat the Copy/Paste GUID process for TDS.Master.Content and TDS.Master.System

my-project-tds-master-get-content7

Once you have repeated the process for all TDS.Master.* projects and populated them with Sitecore items, your VS solution should look like this :

my-project-tds-master-get-content9

That’s it for now, in the next post we will configure Sitecore Glass Mapper, TDS with T4 templates and auto-code generation for the Sitecore data templates.

Stay tuned.

Thanks

 

Related Blogs

  1. Part 1 – Continuous Integration – Why your Sitecore project deployments must be automated ?
  2. Part 2 – Setup and Configure Visual Studio Sitecore Project
  3. Part 3 – Setup and Configure TDS
  4. Part 4 – Setup Sitecore Glass
  5. Part 5 – Setup Source Control (Git)
  6. Part 6 – Setup QA Server, DB server and CI server
  7. Part 7 – Setup Continuous Integration using Team City
  8. Part 8 – Setup Slack Notifications with TeamCity and Bitbucket