Limiting #Sitecore Field Descriptions Using a Partial Class and #GlassMapper

Using a partial class to manipulate/change glass mapper model fields is common. I recently had to do this and thought this little tip for shortening descriptions fields would be come in handy for others. In this case an event listing was displayed. If the description was longer than 50 we would truncate the text and display it with the three dots at the end.

To add the partial class was simple. The key here is having the namespace the same name as you do in the generated model that Glass Mapper generated. If the namespace differs than the partial will not work since it cannot match the class you are adding additional functionality too. In my case ReSharper warns me the namespace path does not match of where I placed my partial class, but that is okay as I know it matches what is in the generated model.

This is what the partial class syntax looks like:

using System;

using Glass.Mapper.Sc;

using Glass.Mapper.Sc.Configuration.Attributes;

namespace Events.Models.sitecore.templates.Feature.Events


public partial class Event


private string _mainContentDescription;


public virtual string MainContentDescription




return _mainContentDescription;




ISitecoreService service = new SitecoreService(Sitecore.Context.Database);

var eventsettings = service.GetItem<EventsSettings>(Constants.Items.EventsSettings);

if (!int.TryParse(eventsettings.Text_Limit, out int textlimit))


textlimit = 50;


_mainContentDescription = value;

_mainContentDescription = !string.IsNullOrEmpty(MainContentDescription) ? TruncateAtWord(MainContentDescription, textlimit) : MainContentDescription;



public static string TruncateAtWord(string value, int length)


if (value == null || value.Length < length || value.IndexOf(” “, length, StringComparison.Ordinal) == -1)

return value;

return value.Substring(0, value.IndexOf(” “, length, StringComparison.Ordinal))+(“…”);




When we go ahead and use it in the view this is what the syntax will look like.

<div class=”desc”>@Html.Raw( Model.MainContentDescription )</div>

Your description should look something like this one done:

This is a description that I don’t want to show it…

There are many possibilities you can do with partial classes and glass mapper. In this case this saved me the time of truncating the characters in the view or creating a separate model were I would have to read the descriptions in and truncate them that way and send that model to my view for example. This is a clean and easy solution and I can use the existing generated glass model so this limits the changes I normally would have to make.

So That is What the Custom Data Property is for. #Sitecore #Hedgehog #TDS and Glass Mapper Model Generation.

Glass and TDS makes our development life easier so we should try and use every feature we can. Right? One of my favorite features is generating a list of Glass models instead of GUIDS. A lot of you probably know this, but there is always times where someone does not. I wanted to document this so this can help others.

Select the field that is a Treelist or whatever multi list field.

In the properties under Code Generation you need to specify the type of list you want returned with model.

In my case:


Now Glass will auto generate and you will get the actual Glass Item Model instead a list of GUIDS.


public partial class
SectionLinks  : GlassBase, ISectionLinks



 /// The Section List field.


<para>Field Type: Treelist</para>          

<para>Field ID: e09af999-37fa-42a3-98b7-1ffb802413c2</para>

<para>Custom Data: type=IEnumerable<Feature.SectionLink.Models.sitecore.templates.Feature.SectionLink.SectionLink></para>


                           [global::System.CodeDom.Compiler.GeneratedCodeAttribute(“Team Development for Sitecore –”, “1.0”)]


IEnumerable<Feature.SectionLink.Models.sitecore.templates.Feature.SectionLink.SectionLink> Section_Lists  {get; set;}


In my controller I just call it this way and I have my list to run through the ForEach:

viewModel = sitecoreservice.Cast<ISectionLinks>(Sitecore.Context.Item,inferType:true).Section_Lists.ToList();

So that is it. Let me know if there is something else we can do with this property. I would like to know.