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;

[SitecoreField(IEventConstants.MainContentFieldName)]

public virtual string MainContentDescription

{

get

{

return _mainContentDescription;

}

set

{

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s