Sitecore Glass Mapper Regenerate Code not working for Color (or custom) Field

If we are using Glass Mapper ORM to convert the Sitecore template to modal properties, some of the custom fields (like color field) downloaded from Sitecore market place or custom field types are not able to retrieve the content from the Sitecore Item.

For example,

We have color field in Sitecore which will be converted into c# property by Glass mapper will return null values in the view like shown below.

Resolution –

The code written in this blog was tested for Sitecore 8.0, 8.2. Other versions of Sitecore May have different API code.

The TDS regenerate code of the Sitecore templates will create template attributes for all the fields defined in the templates. The data types like int or string will be retrieved according to the field types defined in the template. For example, rich area text field will be converted into string in the template. For the fields like color field which is downloaded from market place, Glass mapper will regenerate the items as “object” and not “string”.

When Glass mapper cannot detect the type of the Sitecore field, it will simply create that field as “object” in the public partial interface.

The solution is to convert the object into string which will retrieve the color value from Sitecore color field like shown below.

For Code generation templates to convert object to string  , Navigate to the CTABackgroundColor field in Visual studio and navigate to properties and add type=string in Custom Data field of Sitecore field.


Now In Visual Studio, Click on regenerate code for all items and it should be added as string in auto generated c# template