Separating pages in a transformation

   —   
If you want to show pages in a repeater, categorized by the first letter of their name (or by the year when they were created), and you do not want to use hierarchical transformations because of the complexity or because all your pages may be present in one folder without hierarchy, keep reading!

Let's imagine the following - you have a large number of pages and you are using a Repeater to display them. You are ordering your pages alphabetically, however, you would like to separate pages that start with the letter A from those that start with the letter B and so on to make your page well-arranged.

One option is to make use of hierarchical transformations. However, this requires you to have the hierarchical structure in the content tree already, or requires some advanced database queries to construct the perfect data source.

The other option is to enhance your current transfromations, so that they can "remember" what a specific value of the previously repeated page looked like. We will make use of the ContextHelper, which is able to store data in the HttpContext.Current.Items. Basically, for each repeated page, if the new value is different than the previous one, we will output the "separator" (or "header" if you'd like) and store the new value.

One thing to remember is this: if you want to separate pages by name, first letter or something similar, sort pages alphabetically (in the content tree, or better in the repeater). If you want to separate them by the year in which they were created, then you need to organize them by date.

I am sure you'll come up with many useful modifications of this code, so please feel free to share them below!


The structure of your Content tree

The result on your page

The transformation

<script runat="server"> public string GetHeaderByFirstLetter(string key, string newValue) { string oldValue = (string)(CMS.Helpers.RequestStockHelper.GetItem(key) ?? String.Empty); if (oldValue == string.Empty || newValue[0] != oldValue[0]) { CMS.Helpers.RequestStockHelper.Add(key, newValue); return String.Format("<div class='MyHeader'>{0}</div>", newValue[0]); } return string.Empty; } </script> <%# GetHeaderByFirstLetter("FirstLetterKey",Eval<string>("DocumentName")) %> <p> DocumentName: <%# Eval("DocumentName") %><br /> DocumentCreatedWhen: <%# Eval("DocumentCreatedWhen") %><br /> </p>

The transformation for separating by year

<script runat="server"> public string GetHeaderByYear(string key, DateTime newValue) { DateTime oldValue = (DateTime)(CMS.Helpers.RequestStockHelper.GetItem(key) ?? default(DateTime)); if (oldValue == default(DateTime) || newValue.Year != oldValue.Year) { CMS.Helpers.RequestStockHelper.Add(key, newValue); return String.Format("<h3>{0}</h3>", newValue.Year); } return string.Empty; } </script> <%# GetHeaderByYear("YearKey",Eval<DateTime>("DocumentCreatedWhen")) %> <p> DocumentName: <%# Eval("DocumentName") %><br /> DocumentCreatedWhen: <%# Eval("DocumentCreatedWhen") %><br /> </p>
Share this article on   LinkedIn

David Komárek

As the Lead Product Manager, I am eager to get every feedback there is, especially related to digital marketing, content authoring, automation, and AI.