Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Per Magne Skuseth
Jan 27, 2016
  4293
(0 votes)

EPiServer Find: Index blocks in XhtmlString

Many users use blocks in their XhtmlString properties. However, when an XhtmlString field is being indexed by EPiServer Find, the block content does not get included in the field value. This could lead to users not getting relevant search hits if the term they are searching for resides in a block inside an XhtmlString field.
Below you will find an example on how you could include the block text content to the XhtmlString field value in the EPiServer Find index.

I have written an extension method for XhtmlStrings that returns a string with both block text content and the static text content. It loops through each fragment of the property and appends the values using a StringBuilder

   1: public static string ToBlocksIncludedString(this XhtmlString xhtmlString)
   2: {
   3:     var sb = new StringBuilder();
   4:     if (xhtmlString != null && xhtmlString.Fragments.Any())
   5:     {
   6:         var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
   7:         foreach (IStringFragment fragment in xhtmlString.Fragments.GetFilteredFragments(PrincipalInfo.AnonymousPrincipal))
   8:         {
   9:             // the content fragments contains the referenced blocks
  10:             if (fragment is ContentFragment)
  11:             {
  12:                 var contentFragment = fragment as ContentFragment;
  13:                 if (contentFragment.ContentLink != null &&
  14:                     contentFragment.ContentLink != ContentReference.EmptyReference)
  15:                 {
  16:                     var referencedContent = contentLoader.Get<IContent>(contentFragment.ContentLink);
  17:                     sb.Append(referencedContent.SearchText() + " ");
  18:                 }
  19:             }
  20:             else if (fragment is StaticFragment)
  21:             {
  22:                 // ... and the static fragments contains the static text in the XhtmlString
  23:                 var staticFragment = fragment as StaticFragment;
  24:                 sb.Append(staticFragment.InternalFormat + " ");
  25:             }
  26:         }
  27:     }
  28:     return sb.ToString();
  29: }

 

Include the value in the SearchText field

If you are using Unified Search, you should add the new value to the SearchText field. For IContent, the SearchText field is a combination of all string-based properties from the current content that has not been marked with [Searchable(false)], and will automatically added to the index by standard conventions.
Override the field by adding a property named SearchText on your content type.

   1: public class StandardPage : SitePageData
   2: {
   3:     [Searchable(false)]
   4:     public virtual XhtmlString MainBody { get; set; }
   5:  
   6:     [RemoveHtmlTagsWhenIndexing]
   7:     public string SearchText => this.SearchText() + " " +  MainBody.ToBlocksIncludedString();
   8: }

Make sure that the original XhtmlString property has “Searchable” set to false, or you’ll get both the ToBlocksIncludedString value and the standard value of the XhtmlString property added to the SearchText field.

 

Changing the default indexing behavior for all XhtmlStrings

If you always want to include the block content for you XhtmlStrings, you could do this by adding a few conventions in an initializable module. In the example below, the standard value, named “AsViewedByAnonymous” in the index, is replaced by the new value.

   1: [InitializableModule]
   2: [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
   3: public class FindFieldInitialization : IInitializableModule
   4: {
   5:     public void Initialize(InitializationEngine context)
   6:     {
   7:         SearchClient.Instance.Conventions.ForType<XhtmlString>().ExcludeField(x => x.AsViewedByAnonymous());
   8:         SearchClient.Instance.Conventions.ForType<XhtmlString>().IncludeField(x => x.ToBlocksIncludedString());
   9:         SearchClient.Instance.Conventions.ForType<XhtmlString>().Field(x => x.ToBlocksIncludedString()).Modify(x => x.PropertyName = "AsViewedByAnonymous" + TypeSuffix.String);
  10:     }
  11:  
  12:     public void Uninitialize(InitializationEngine context){}
  13: }
Jan 27, 2016

Comments

Please login to comment.
Latest blogs
Optimizely Configured Commerce and Spire CMS - Figuring out Handlers

I recently entered the world of Optimizely Configured Commerce and Spire CMS. Intriguing, interesting and challenging at the same time, especially...

Ritu Madan | Mar 12, 2025

Another console app for calling the Optimizely CMS REST API

Introducing a Spectre.Console.Cli app for exploring an Optimizely SaaS CMS instance and to source code control definitions.

Johan Kronberg | Mar 11, 2025 |

Extending UrlResolver to Generate Lowercase Links in Optimizely CMS 12

When working with Optimizely CMS 12, URL consistency is crucial for SEO and usability. By default, Optimizely does not enforce lowercase URLs, whic...

Santiago Morla | Mar 7, 2025 |

Optimizing Experiences with Optimizely: Custom Audience Criteria for Mobile Visitors

In today’s mobile-first world, delivering personalized experiences to visitors using mobile devices is crucial for maximizing engagement and...

Nenad Nicevski | Mar 5, 2025 |

Unable to view Optimizely Forms submissions when some values are too long

I discovered a form where the form submissions could not be viewed in the Optimizely UI, only downloaded. Learn how to fix the issue.

Tomas Hensrud Gulla | Mar 4, 2025 |

CMS 12 DXP Migrations - Time Zones

When it comes to migrating a project from CMS 11 and .NET Framework on the DXP to CMS 12 and .NET Core one thing you need to be aware of is the...

Scott Reed | Mar 4, 2025