SharePoint 2013 – Display the first 100 characters of a Publishing Page Content field using Display Templates

How do you display the first set of characters of a Publishing Page in a display template?

Display templates

I like the usage of Display templates but every now and then there are problems that should be easy to solve that are not. Well, the problem in this article is actually  one of those easily solved problems. Once you know how.

I’ve got a page in my Office 365 SharePoint online environment. I’m using the page content field to collect the article content.

So in my Display Template I’m adding PublishingPageContentOWSHTML

<mso:ManagedPropertyMapping msdt:dt=”string”>’PublishingPageContentOWSHTML’:’PublishingPageContentOWSHTML'</mso:ManagedPropertyMapping>

Then within the JavaScript section of the display template  I’m collecting the summary:

var summary = ctx.CurrentItem.PublishingPageContentOWSHTML;

and I’ve got my html code in the summary variable. If I now get the substring of the summary to get my first 100 characters I might end up with a bit of a mess and most likely nowhere  near 100 characters.

“<p>​Today we have launched the new intranet …. </p><p><span class=\”ms-rteStyle-Normal\”>Lorem ipsum”

Within my display template I’m including some html to display the summary

_#= summary =#_

This will now appear in SharePoint as:

TodayWeLaunched

So now the solution.

first I’m creating a div element

var tag = document.createElement(‘div’);

Then I’m setting the innerHTML of the div that I just created

tag.innerHTML = ctx.CurrentItem.PublishingPageContentOWSHTML;

first set of characters of a Publishing Page

And now the big trick. Set my summary variable to the innerText

var summary = tag.innerText.substring(0,100) + “… “;

So all together this gives me:

var tag = document.createElement(‘div’);
tag.innerHTML = ctx.CurrentItem.PublishingPageContentOWSHTML;
var summary = tag.innerText.substring(0,100) + “… “;

And SharePoint now displays the full first 100 characters:

LaunchedOk

Discover more from SharePains

Subscribe to get the latest posts sent to your email.

Avatar of Pieter Veenstra

Is your business still running on paper trails, sprawling Excel files, or ageing Access databases? There's a better way — and I can show you exactly what it looks like. I'm the Technical Director of Vantage 365, a Microsoft solutions consultancy working with clients across the UK, the Netherlands, and worldwide. For over 30 years I've been turning messy, manual business processes into clean, automated systems that save time, reduce errors, and give teams the visibility they need to make better decisions. SharePains is not just any blog run by a Microsoft MVP. Have you ever used Try-Catch in Power Automate? The original post about Try-Catch in Power Automate can still be found on this site, https://sharepains.com/2018/02/07/try-catch-finally-in-power-automate-flow/ Or have you ever used the Pieter’s method to avoid variables and speed up your flows? https://sharepains.com/2020/03/11/pieters-method-for-advanced-in-flows/ You can contact me using contact@sharepains.com

Related Posts

3 thoughts on “SharePoint 2013 – Display the first 100 characters of a Publishing Page Content field using Display Templates

  1. Interesting. I’ve used regex to remove the html tags and then gave a substring in what became. I did this so that if there is a link in the content does not break my layout

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from SharePains

Subscribe now to keep reading and get access to the full archive.

Continue reading