2 min read
Kentico 12 MVC dev hacks: Smart search & widget content
When optimizing your Kentico 12 MVC Smart Search index, you may run into a snag if you’re trying to index content stored within widgets. Learn Ntara’s development hack and how to avoid false positives.
Kentico 12 MVC is a flexible framework for website development. It’s incredibly easy for content marketers to use the Page Builder and create great looking pages on their own. However, by default in the current version, the content stored within widgets built using Page Builder cannot be added to a Smart Search index.
To work around this, we could use a Pages Crawler type index, but that could result in false positives and less relevant results due to the header/footer being indexed for each page. Kentico is aware of this limitation and they currently have this slated as part of Milestone 2 on their Kentico 2020 roadmap.
Since Kentico 2020 isn’t scheduled to be released for another six months or so, we developed a custom solution that would meet our client’s needs in the interim.
After analyzing a few ways we could handle this, we landed on customizing the content of the search index as the most bang for our buck.
We can reference the Kentico documentation for a reference on how to customize the content of a search index.
First, we need to implement an event handler for DocumentsEvents.GetContent.Execute, which occurs right before a page’s content is written to the Smart Search index. To do this, we create a custom module class as described here. This module needs to be accessible by both the Admin and the Website, so it is created in a library that is shared across both solutions.
The second parameter in the event handler is of type DocumentSearchEventArgs which gives us access to the TreeNode. This also gives us the ability to inject custom content prior to it being written to the index. We need to retrieve the content from the DocumentPageBuilderWidgets column from the TreeNode and parse the JSON. To handle this parsing, we can use Json.NET which will allow us to easily ignore the extra content in the JSON object that we don’t care about.
In case we need to easily add more content to this page for indexing, we can create an extension method on DocumentSearchEventArgs to pass in the content to be appended.
Kentico MVC’s flexibility has added great value to our clients–it’s fast and nimble to create new, great looking pages. With this Kentico dev hack, our end users can also enjoy more relevant site search results. And with the Kentico 2020 release, it’ll be even easier to optimize.
Have a question about Kentico development? Let’s talk.