Tag Archives: item name

Exposing Sitecore Item Properties when Using Code Generation with Glass.Mapper

When initially setting up TDS and with Glass.Mapper code generation in your solution, by default you get the initial properties from the Sitecore item:

  • Id – The ID for the item
  • Language – The language for the item
  • Version – The version of the item

However, what if you want something like the item name available to you like you have available when going through the standard Sitecore API? Don’t worry, the Glass.Mapper team has you covered and you can extend the GlassBase and IGlassBase respectively to get you your desired results.

After you got your initial setup of Code Generation setup with TDS, you simply go to your “glassv3header.tt” file and there you can extend the GlassBase/IGlassBase. However, if you start coding in this file you will have no Visual Studio Intellisense, so I actually wrote code in my ViewModel so I could see what was available in the SitecoreInfoType object. Below are a few:

SitecoreInfoType.Name
SitecoreInfoType.DisplayName
SitecoreInfoType.ContentPath

Once you can see what is available via Intellisense you can set them in the “glassv3header.tt” file appropriately when generating the code as such.

public partial interface IGlassBase
{
[SitecoreId]
Guid Id{ get; }


[SitecoreInfo(SitecoreInfoType.Language)]
Language Language{ get; }


[SitecoreInfo(SitecoreInfoType.Version)]
int Version { get; }

}

public abstract partial class GlassBase : IGlassBase
{

[SitecoreId]
public virtual Guid Id{ get; private set;}


[SitecoreInfo(SitecoreInfoType.Language)]
public virtual Language Language{ get; private set; }


[SitecoreInfo(SitecoreInfoType.Version)]
public virtual int Version { get; private set; }


[SitecoreInfo(SitecoreInfoType.Url)]
public virtual string Url { get; private set; }


[SitecoreInfo(SitecoreInfoType.Name)]
public virtual string Name { get; private set; }

}

This will work to get the item name. However, I noticed that ASP.NET MVC when using Html.Helpers with names of “name” will pull in the Item name into the textbox as the value. Hence, I had to change from “Name” to “ItemName” as such and I was back happy coding again and relieved myself of regression testing after making this change. Below is my modification to get the item name property:

[SitecoreInfo(SitecoreInfoType.Name)]
public virtual string ItemName { get; private set; }

Glass.Mapper, TDS, and Code Generation is a phenomenal tool and highly recommended for rapid development with Sitecore. Play around with it and you can get what you need every time. Happy Holidays!