Sergii Vorushylo
Jul 31, 2015
  7181
(6 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

Jul 31, 2015 09:16 PM

Please ship this in a seperate package. We might have our own search blocks, and definately our own markup and styling.

I would rather see you guys focusing on fixing bugs instead of doing our work of delivering content types.

Aug 2, 2015 05:31 PM

Thank you for the feedback, Johan. If there are any particular bugs, which you think need to be prioritized, please upvote them in the bug list: http://world.episerver.com/support/Bug-list/

Henrik Fransas
Henrik Fransas Aug 3, 2015 08:48 PM

Sergil, here is a few that is holding my customers back:

  • Using xform in self optimizing block does not work
  • CMO report link is missing file suffix
  • (a feature more than a bug) Click tracking does not work "out of the box" for more than unified search (see my blog post on how to work around it)
  • ....

Some feature request on things that is more important than this to be built in:

  • Property: List Of Strings
  • Property: Change url/link to allow to add a url like this "/search" to support custom url routing
  • Property: List Of Many Other Things (List Of T)

Specific for Find:

  • Able to get most search request of other stuff from the statistics part through the api
  • Slim down the unified search response, it is returning way to much today

Aug 4, 2015 11:17 AM

Thank you, Henrik. I will hand over this to our product management.

(╯°□°)╯︵ ┻━┻
(╯°□°)╯︵ ┻━┻ Aug 6, 2015 12:17 AM

Johan is right; please don't ship this with the product. This will only cause confusion with the users, and therefore must be hidden in every environment -> more manual labour for me :(

Ulrika Lindeberg
Ulrika Lindeberg Sep 17, 2015 02:23 PM

If you want to remove the block for editors, you can add an InitializableModule to your solution:

[InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class HideCustomizedSearchBlock : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            var typerepo = ServiceLocator.Current.GetInstance();
            var searchblocktype = typerepo.Load().CreateWritableClone();
            ((ContentType)searchblocktype).IsAvailable = false;
            typerepo.Save((ContentType)searchblocktype);

        }

        public void Preload(string[] parameters) { }

        public void Uninitialize(InitializationEngine context)
        {
            //Add uninitialization logic
        }
    }

Peter Gustafsson
Peter Gustafsson Oct 21, 2015 07:46 PM

+1 for moving this into a separate package.

Peter Healy
Peter Healy Feb 7, 2017 05:52 PM

Just had a client asking us about a content block that does not appear to work, and we'd missed it out of the guide we wrote for them. I had a look and thought: "where the hell did that come from?".

+2 for moving this into a separate package.

Dec 27, 2021 06:43 AM

Can anyone suggest me a solution for the issue mentioned in the post ?  https://world.optimizely.com/forum/developer-forum/Search/Thread-Container/2021/12/-customizedsearchblock-not-considering-current-culture-/

Please login to comment.
Latest blogs
Optimizely SendGrid SMTP host is deprecated

SendGrid is a services for sending email that is included in Optimizely DXP. Previously smtp.episerver.net was the recommended SMTP server to use,...

Tomas Hensrud Gulla | Dec 4, 2022 | Syndicated blog

Hosting Optimizely CMS 12 on Docker Engine

Since Optimizely CMS can now be deployed as a Docker container, here is a demonstration of building, running and scaling an Optimizely CMS 12 site ...

Stefan Holm Olsen | Dec 4, 2022 | Syndicated blog

How to use CacheTagHelper with content areas in Optimizely CMS 12

I might be going out on a limb here - if you have a better solution, feel very free to share it!  Upgrading your Optimizely web application from .N...

Andreas J | Dec 2, 2022

The 1001st Piece in your 1000 Piece Puzzle: .NET Default Interface Functions

I was recently working with a client who wanted a reasonably large subsystem added to Optimizely that would add automated management to their...

Greg J | Nov 28, 2022 | Syndicated blog

Video Demonstration, creating a CMS12 Alloy Sample Site

Hey All Below you will find a quick video demonstration on how to install a local version of Alloy Sample based on CMS12 / .Net 6. As you will see ...

Minesh Shah (Netcel) | Nov 28, 2022

How to create an admin user I Optimizely CMS – with Episerver CLI

In this blog post I’ll show how to create an admin user for Optimizely CMS in a new environment where you don’t have access to the admin interface.

Ove Lartelius | Nov 28, 2022 | Syndicated blog