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


Jul 31, 2015
  4029
(0 votes)

Customized Search Block in EPiServer Find

In the latest version of EPiServer Find, we have added a new feature for editors. It’s Customized Search Block. This feature provides editors with a block that shows a dynamic list of links to the content matching a search criteria. Search for the content is based on EPiServer Find's powerful capabilities. This block can be used, for example, to display related content such as “you may also like” links etc.

Settings

The block settings let editors choose how to display the list as well as fine tune the search query.

Basic block settings in On-Page editing mode:

All settings for the block available in the All Properties mode:

Details about each parameter can be found in the documentation.

Customization

You can adjust the block's appearance by adding CSS classes described in the styling guide for developers. What if we want to customize a block's rendering? There are several ways to do it, but first let’s take a look on how the block is built. Here is a class diagram for content types:

CustomizedSearchBlock has three properties and two of them are also blocks: CustomizedSearchSettings and CustomizedSearchAdvancedSettings. The last two blocks can’t be instantiated by editors and are used only to group the settings. Such grouping lets you use On-Page editing for basic settings and provides visual controls grouping in All properties mode.

All logic for creating a query to EPiServer Find is placed in EPiServer.Find.Blocks.ICustomizedSearchBlockService implementation. It has two methods:

public interface ICustomizedSearchBlockService
{
ITypeSearch<ISearchContent> CreateSearchQuery(CustomizedSearchBlock currentBlock);
CustomizedSearchResultsViewModel CreateViewModel(UnifiedSearchResults results, bool includeDescription);
}
 

CreateSearchQuery method accepts CustomizedSearchBlock instance as an argument and returns ITypeSearch. The returned value is an EPiServer Find search request prepared with a query and filters according to the blocks settings and ready to call GetResult on it. CreateViewModel method takes UnifiedSearchResults and returns a view model for the block:

namespace EPiServer.Find.Blocks.Models
{
public class CustomizedSearchResultsViewModel
{
public IList<SearchListItem> Items { get; set; }
public bool IncludeDescription { get; set; }
}

public class SearchListItem
{
public string Title { get; set; }
public string Url { get; set; }
public string Description { get; set; }
}
}

Creating a new renderer

The easiest way to provide a new rendering for the block is to create a new MVC Controller and a View. Because of implementation specificity, the new Controller should inherit from EPiServer.Find.Blocks.Controllers.CustomizedSearchSettingsBlockController:

using System.Web.Mvc;
using EPiServer.Find.Blocks;
using EPiServer.Find.Blocks.Controllers;
using EPiServer.Find.Blocks.Models.ContentTypes;

namespace CustomizedSearchBlock.Controllers
{
public class CustomRenderingSearchBlock : CustomizedSearchSettingsBlockController
{
public CustomRenderingSearchBlock(ICustomizedSearchBlockService customizedSearchBlockService, IContextHelper contextHelper)
: base(customizedSearchBlockService, contextHelper){}

public override ActionResult Index(CustomizedSearchSettings currentBlock)
{
var model = CreateSearchResultsViewModel(currentBlock);
return PartialView("Index", model);
}
}
}

This controller is responsible for rendering the actual list. The list's heading is rendered by the parent blocks controller.

Then, you need to add a view:

@model EPiServer.Find.Blocks.Models.CustomizedSearchResultsViewModel
<dl>
@foreach (var link in Model.Items)
{
<dd>
<a href="@link.Url">@link.Title</a>
</dd>
}
</dl>
The last step is to set the new controller as a default display template for the CustomizedSearchSettings block in Admin mode. To find the block in Admin mode, you need to open settings for the Customized search block type first and click the CustomizedSearchSettings link in the Type column:

In the settings view for the CustomizedSearchSettings block, click the Settings button and choose the new controller in the MVC Template drop down. Now, the new renderer should be used when the block is displayed.

If the data provided in the CustomizedSearchResultsViewModel is not enough, for example if we want to render an image together with a link, you can access search results directly using the ICustomizedSearchBlockService and creating your own view model. There is an example of such implementation on GitHub as well as the code for examples above.

Jul 31, 2015

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