Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Simon J Ovens
Feb 17, 2017
  2313
(0 votes)

Setting up Episerver Find on a development machine using Autofac

Setting up Episerver Find is straight forward but I did run into a few steps that could stump new players.

The first two following steps are well documented so I will not go into them.

Step 1: Create an Episerver Find account online.

Step 2: Add the web config details to your project.

Step 3: Next we add the Autofac dependency Injection code into our Global.asax.cs file, most of the code goes into the Application_Start() method that already exist. 
Note: Below I am registering all Controllers in the application using builder.RegisterControllers(typeof(EpiServerApplication).Assembly) so just changed the EpiServerApplication class to match your application. 
We then register our search client class and set the dependency resolver using Autofac.
This means we only instantiate the Client class once and then inject it into our controllers, which improves the applications performance.  

    public class EPiServerApplication : EPiServer.Global
    {
        protected void Application_Start()
        {
            var builder = new ContainerBuilder();
            builder.RegisterControllers(typeof(EPiServerApplication).Assembly);
            builder.Register(x =>
                CreateSearchClient()).As<IClient>().SingleInstance();
            var container = builder.Build();
            DependencyResolver.SetResolver(
                new AutofacDependencyResolver(container));

            //Standard MVC stuff
            AreaRegistration.RegisterAllAreas();
        }

        private static IClient CreateSearchClient()
        {
            var client = Client.CreateFromConfig();
            //Any modifications required goes here    
            return client;
        }
    }

 

Step 4: On the Episerver Alloy template the code above didnt compile because it was missing some important DLLs, to fix this we now need to add the Autofac.dll and Autofac.Integration.Mvc.dll to our project.

In Visual Studio go to Tools > NuGet Package Manager > Package Manager Console

In the Package Manager Console enter the following:

PM > Install Package Autofac

Then run

PM > Install Package Autofac.Mvc5   

The code we entered above will now resolve and you should be able to build your project without errors.

Step 5: Now in a Controller we need to add an IClient property, which as you can see below I called FindServiceClient. 
Then add a constructor with the IClient parameter that we are injecting into and set it to the FindServiceClient property. 
Now we can write queries against the Find service as follows, this code is from a block controller that returns pages:

    public class TopRatedHomesBlockController : BlockController<TopRatedHomesBlock>
    {
        public IClient FindServiceClient { get; private set; }

        public TopRatedHomesBlockController(IClient client)
        {
            FindServiceClient = client;
        }

        public override ActionResult Index(TopRatedHomesBlock currentBlock)
        {
            var contentResult = FindServiceClient.Search<AccommodationPage>()
                            .Filter(h => h.Rating.GreaterThan(4))
                            .GetContentResult();

            var model = new TopRatedHomesBlockModel
            {
                Heading = currentBlock.Heading,
                ContentResult = contentResult
            };

            return PartialView(model);
        }
    }

Step 6: In the code above I am using an extension method to get PageData, the AccommodationPage class inherits StandardPage which inherits SitePageData which inherits PageData, the extension method used is called GetContentResult() .

To use this extension method we must add the following using to our class:

using EPiServer.Find.Cms;    

Completed: That wraps up how to setup the powerful Episerver Find!  
  

Feb 17, 2017

Comments

Please login to comment.
Latest blogs
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog