Manoj Kumawat
Jul 26, 2019
(5 votes)

Making use of TypeConventionBuilder to include a field with Find

The prerequisite for this post is a basic knowledge of Episerver.Find.

Yesterday I came accross with an issue with Episerver.Find filters. If you've ever faced issues with XhtmlString values with Episerver.Find then this blog post may help you.

I had an XhtmlString as one of the properties within the block as follows - 

 Name = "Title",
 Description = "Title to display.",
 GroupName = SystemTabNames.Content,
 Order = 400
public virtual XhtmlString Title { get; set; }

And I wanted to sort the blocks based on Title (typeof(XhtmlString)) with Find query - 

.OrderBy(c => c.Title)

I experienced this wouldn't sort the blocks based on OrderBy Title. Because the string type here would return Xhtml elements and ultimately this would not sort it properly. 

In case if I had the Title as plain string property in block then it should have sorted it as expected. But I didn't like the idea to add any additional field of type string instead of XhtmlString to existing block.

So what should have been done?

Keeping this question in mind, I posted a forum post and as expected this finally put the results in. I can't thank enough to Paul to sort this out for me. 

Here is how it worked

If you could get rid of those HTML elements anyhow within the XhtmlString then it would definitely be a way to go - 

Step 1

Creating an extension method to strip out HTML from XhtmlString - 

 public static class SearchExtensions
        public static string StripHtml(this XhtmlString htmlField)
            return htmlField == null ? string.Empty : EPiServer.Core.Html.TextIndexer.StripHtml(htmlField.ToHtmlString(), 1000);

This is using an existing method TextIndexer class to strip out those HTML elements. Which returns up to 1000 characters (which should be easily sufficient for sorting).

Step 2

Create an InitializableModule that would use TypeConventionBuilder to include a field in Episerver.Find Indexed contents and use the extension method created above - 

    public class InitialiseSearch : IInitializableModule
        public void Initialize(InitializationEngine context)
            SearchClient.Instance.Conventions.ForInstancesOf<MyCustomBlock>().IncludeField(x => x.Title.StripHtml());

        public void Uninitialize(InitializationEngine context)

This will include field to find index with the key - Title.StripHtml$$string and this is the key that would do our job done.

Step 3

Modifying Find query to use this extension method - 

.OrderBy(c => c.Title.StripHtml())
Step 4

Now you would need to reindex your contents with the scheduled job from Episerver's Admin area - EPiServer Find Content Indexing Job

After you've reindexed, It modifies the way the content is indexed, adding in a field to the content items in the index called "Title.StripHtml$$string". When you search or order on Title.StripHtml(), it doesn't run the extension method, instead it looks for the "Title.StripHtml$$string" field. If you haven't reindexed, that field won't be there.

Verifying the index - 

Go to Episerver Find/overview and click on Explore and then search for your block. This should have the field added to the Find index and look closely it is a plain string now.

Have a great day!

Jul 26, 2019


Praful Jangid
Praful Jangid Jul 26, 2019 10:21 AM

Good Job Manoj. I appreciate you shared this finding as a blog post. That will help others too and will save their time. :)

Manoj Kumawat
Manoj Kumawat Jul 26, 2019 10:27 AM

Sure thing Praful!

Appreciate your time as well :)

Jul 29, 2019 12:28 PM

I would suggest reading my blog post on GetContent vs GetContentResult GetContentResult under the covers reloads data using the IContentLoader service to get back fields from IContent. This means that custom data is not reloaded including extension methods, so you'll find when you're querying the HtmlStrip method will be being called for every result again when getting the data back from find which is not ideal from a performance point of view, you just want it to come back from the index where as you've show it's being indexed. I'd suggest projections as the simplest way to get it working then you can switch to GetResult which fully uses everything from the index.

Paul Gruffydd
Paul Gruffydd Jul 29, 2019 05:11 PM

Glad you got it working and good to see you taking the time to write it up.

@Scott - That's a useful tip for situations when you need to retrieve the additional data though in this instance the data provided by the extension method is only used for sorting the results so HtmlStrip() should only be called when the content is being indexed, not when it's being retrieved.

Manoj Kumawat
Manoj Kumawat Jul 30, 2019 07:26 AM

Hey @Scott,

Thanks for pointing that out and a great blog post resource. Pretty much everything I wanted to know. 

However, In this instance as Paul suggested and tested while debugging, It never hits the extension method at the time of data being retrieved. Instead when I am going to index the contents it hits it for every result (I guess this is the moment when it is adding the key for Title in Find Index using extension method). 

Just want to be sure if this won't impact performance?

Jul 30, 2019 08:13 AM

Yes sorry I think Paul is right, this will only affect if the data is coming back. As you've got this in the Sort part this will be evaluated in Elasticsearch. Apologies for the confusion although it's always good to know about these things if you ever need to bring back the custom data.

Thanks, Scott

Please login to comment.
Latest blogs
Customizing Property Lists in Optimizely CMS

Generic property lists is a cool editorial feature that has gained a lot of popularity - in spite of still being unsupported (officially). But if y...

Allan Thraen | Oct 2, 2022 | Syndicated blog

Content Delivery API – The Case of the Duplicate API Refresh Token

Creating a custom refresh provider to resolve the issues with duplicate tokens in the DXC The post Content Delivery API – The Case of the Duplicate...

David Lewis | Sep 29, 2022 | Syndicated blog

New Optimizely certifications - register for beta testing before November 1st

In January 2023, Optimizely is making updates to the current versions of our certification exams to make sure that each exam covers the necessary...

Jamilia Buzurukova | Sep 28, 2022

Optimizely community meetup - Sept 29 (virtual + Melbourne)

Super excited to be presenting this Thursday the 29th of September at the Optimizely community meetup. For the full details and RSVP's see the...

Ynze | Sep 27, 2022 | Syndicated blog

Preview multiple Visitor Groups directly while browsing your Optimizely site

Visitor groups are great - it's an easy way to add personalization towards market segments to your site. But it does come with it's own set of...

Allan Thraen | Sep 26, 2022 | Syndicated blog

The Report Center is finally back in Optimizely CMS 12

With Episerver.CMS.UI 12.12.0 the Report Center is finally re-introduced in the core product.

Tomas Hensrud Gulla | Sep 26, 2022 | Syndicated blog