<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><language>en</language><title>Blog posts by Simon J Ovens</title> <link>https://world.optimizely.com/blogs/simon-j-ovens/</link><description></description><ttl>60</ttl><generator>Optimizely World</generator><item> <title>How to get the current page&#39;s category from a block and use Find to search for all pages that match the category.</title>            <link>https://world.optimizely.com/blogs/simon-j-ovens/dates/2017/2/how-to-get-the-current-pages-category-and-using-find-search-for-all-pages-that-match-the-category-from-a-block/</link>            <description>&lt;p&gt;In my application I created a block that showed all related pages based on the parent pages category.&amp;nbsp;&lt;br /&gt;To achieve this I created a new category field called &lt;strong&gt;Location&lt;/strong&gt;, the idea behind this field&amp;nbsp;is to only select one location per page hence the call below to get the &lt;strong&gt;First&lt;/strong&gt;() location. You could use .&lt;strong&gt;In&lt;/strong&gt;() if you required to match against multiple.&lt;br /&gt;In the block controllers &lt;strong&gt;ActionResult()&lt;/strong&gt; method I get the parent page object by using the &lt;strong&gt;ServiceLocator.Current.GetInstance&amp;lt;IPageRouteHelper&amp;gt;() .&lt;/strong&gt;&amp;nbsp;&lt;strong&gt;&amp;nbsp;&lt;/strong&gt;&lt;br /&gt;In the search query you can see that I first &lt;strong&gt;Filter()&lt;/strong&gt; on the &lt;strong&gt;Location&lt;/strong&gt; category field and call &lt;strong&gt;Match()&lt;/strong&gt; to get the pages that match the parent page&#39;s &lt;strong&gt;Location&lt;/strong&gt;.&lt;br /&gt;I then use &lt;strong&gt;Filter() &lt;/strong&gt;and&lt;strong&gt; Match()&amp;nbsp;&lt;/strong&gt;to exclude the parent page from the results based on the &lt;strong&gt;ContentGUID.&lt;/strong&gt; &amp;nbsp;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;Note: The code below has no exception handling for blog readability so this would need to be added in a real world application.&lt;/p&gt;
&lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    public class RelatedHomesBlockController : BlockController&amp;lt;RelatedHomesBlock&amp;gt;
    {
        private readonly IPageRouteHelper _pageRouteHelper;
        public IClient FindServiceClient { get; private set; }

        public RelatedHomesBlockController(IClient client)
        {
            FindServiceClient = client;
            _pageRouteHelper = ServiceLocator.Current.GetInstance&amp;lt;IPageRouteHelper&amp;gt;();
        }

        public override ActionResult Index(RelatedHomesBlock currentBlock)
        {
            var currentPage = (_pageRouteHelper.Page as AccommodationPage);
            var currentPageCategory = currentPage.Location.First();
            var contentResult = FindServiceClient.Search&amp;lt;AccommodationPage&amp;gt;()
                                .Filter(h =&amp;gt; h.Location.Match(currentPageCategory))
                                .Filter(h =&amp;gt; !h.ContentGuid.Match(currentPage.ContentGuid))
                                .GetContentResult();
            var model = new RelatedHomesBlockModel
            {
                Heading = currentBlock.Heading,
                ContentResult = contentResult
            };

            return PartialView(model);
        }
    }&lt;/code&gt;&lt;/pre&gt;</description>            <guid>https://world.optimizely.com/blogs/simon-j-ovens/dates/2017/2/how-to-get-the-current-pages-category-and-using-find-search-for-all-pages-that-match-the-category-from-a-block/</guid>            <pubDate>Sat, 18 Feb 2017 02:13:19 GMT</pubDate>           <category>Blog post</category></item><item> <title>Setting up Episerver Find on a development machine using Autofac</title>            <link>https://world.optimizely.com/blogs/simon-j-ovens/dates/2017/2/setting-up-episerver-find-on-development-machine/</link>            <description>&lt;p&gt;Setting up Episerver Find is straight forward but I did run into a few&amp;nbsp;steps that could stump new players.&lt;/p&gt;
&lt;p&gt;The first two following steps are well documented so I will not go into them.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Create an Episerver Find account online.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Add the web config details to your project.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt;&amp;nbsp;Next we add the Autofac dependency Injection code into our &lt;strong&gt;Global.asax.cs&lt;/strong&gt; file, most of the code goes into the &lt;strong&gt;Application_Start()&lt;/strong&gt;&amp;nbsp;method that&amp;nbsp;already exist.&amp;nbsp;&lt;br /&gt;Note: Below I am registering all Controllers in the application using &lt;strong&gt;builder.RegisterControllers(typeof(EpiServerApplication).Assembly) &lt;/strong&gt;so just changed the &lt;strong&gt;EpiServerApplication &lt;/strong&gt;class to match your application.&amp;nbsp;&lt;br /&gt;We then register our search client class and set the dependency resolver using Autofac.&lt;br /&gt;This means we only&amp;nbsp;&lt;span&gt;instantiate the &lt;strong&gt;Client &lt;/strong&gt;class once and then inject it into our controllers, which improves the applications performance.&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    public class EPiServerApplication : EPiServer.Global
    {
        protected void Application_Start()
        {
            var builder = new ContainerBuilder();
            builder.RegisterControllers(typeof(EPiServerApplication).Assembly);
            builder.Register(x =&amp;gt;
                CreateSearchClient()).As&amp;lt;IClient&amp;gt;().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;
        }
    }&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; On the Episerver Alloy template the code above didnt compile because it was missing some important DLLs, to fix this we&amp;nbsp;now need to add the &lt;strong&gt;Autofac.dll &lt;/strong&gt;and&lt;strong&gt;&amp;nbsp;Autofac.Integration.Mvc.dll&lt;/strong&gt; to our project.&lt;/p&gt;
&lt;p&gt;In Visual Studio go to&lt;strong&gt; Tools &amp;gt; NuGet Package Manager &amp;gt; Package Manager Console&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In the &lt;strong&gt;Package Manager Console &lt;/strong&gt;enter the following:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;PM &amp;gt; Install Package Autofac&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Then run&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;PM &amp;gt; Install Package&amp;nbsp;&lt;span&gt;Autofac.Mvc5&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;/strong&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The code we entered above will now resolve and you should be able to build your project without errors.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt;&amp;nbsp;Now in a Controller we need to add&amp;nbsp;an IClient property, which as you can see below I called &lt;strong&gt;FindServiceClient.&lt;/strong&gt;&amp;nbsp;&lt;br /&gt;Then add a constructor with the IClient parameter that we are injecting into and set it to the &lt;strong&gt;FindServiceClient&lt;/strong&gt;&lt;strong&gt;&amp;nbsp;&lt;/strong&gt;property.&amp;nbsp;&lt;br /&gt;Now we can write queries against the Find service as follows, this code is from a block controller that returns pages:&lt;/p&gt;
&lt;pre class=&quot;language-html&quot;&gt;&lt;code&gt;    public class TopRatedHomesBlockController : BlockController&amp;lt;TopRatedHomesBlock&amp;gt;
    {
        public IClient FindServiceClient { get; private set; }

        public TopRatedHomesBlockController(IClient client)
        {
            FindServiceClient = client;
        }

        public override ActionResult Index(TopRatedHomesBlock currentBlock)
        {
            var contentResult = FindServiceClient.Search&amp;lt;AccommodationPage&amp;gt;()
                            .Filter(h =&amp;gt; h.Rating.GreaterThan(4))
                            .GetContentResult();

            var model = new TopRatedHomesBlockModel
            {
                Heading = currentBlock.Heading,
                ContentResult = contentResult
            };

            return PartialView(model);
        }
    }&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 6: &lt;/strong&gt;In the code above I am using an extension method to get&lt;strong&gt; PageData, &lt;/strong&gt;the&lt;strong&gt; AccommodationPage &lt;/strong&gt;class&lt;strong&gt;&amp;nbsp;&lt;/strong&gt;inherits&lt;strong&gt; StandardPage &lt;/strong&gt;which inherits&lt;strong&gt; SitePageData &lt;/strong&gt;which inherits&lt;strong&gt; PageData, &lt;/strong&gt;the extension method used is called&amp;nbsp;&lt;strong&gt;GetContentResult() .&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To use this extension method we must add the following &lt;strong&gt;using&lt;/strong&gt; to our class:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;using EPiServer.Find.Cms;&amp;nbsp;&lt;/strong&gt;&amp;nbsp;&lt;strong&gt;&amp;nbsp;&lt;/strong&gt;&lt;strong&gt;&amp;nbsp;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Completed: &lt;/strong&gt;That wraps up how to setup the powerful Episerver Find!&lt;strong&gt;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&lt;/strong&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;</description>            <guid>https://world.optimizely.com/blogs/simon-j-ovens/dates/2017/2/setting-up-episerver-find-on-development-machine/</guid>            <pubDate>Fri, 17 Feb 2017 21:29:39 GMT</pubDate>           <category>Blog post</category></item></channel>
</rss>