Jump To …

FindInitialization.cs

using System;
using System.Collections.Generic;
using EPiServer.Find.Cms;
using EPiServer.Find.Cms.Conventions;
using EPiServer.Find.Framework;
using EPiServer.Find.UnifiedSearch;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.Templates.Alloy.Models.Pages;
using EPiServer.Web.Hosting;

namespace EPiServer.Templates.Alloy.Business.Initialization
{
    [ModuleDependency(typeof(Find.Cms.Module.IndexingModule))]
    public class FindInitialization : IInitializableModule
    {

Create list of file types that we want to index. By making it static we avoid re-creating the list each time a file is indexed.

        static IList<string> fileTypesToIndex = new List<string>
            {
                ".pdf", 
                ".doc", 
                ".docx"
            };

        public void Initialize(InitializationEngine context)
        {

By default uploaded files are not indexed. Change that to index all files in all VPP folders that are visible to editors in the file manager.

            FileIndexer.Instance.Conventions.ShouldIndexVPPConvention
                = new VisibleInFilemanagerVPPIndexingConvention();

As we have no need to index images and such we configure the FileIndexer to only index Word and PDF files.

            FileIndexer.Instance.Conventions.ForInstancesOf<UnifiedFile>()
                .ShouldIndex(x => 
                    fileTypesToIndex.Contains(x.Extension)
                    );

Customize the default projection of pages when using Unified Search to include an image.

            SearchClient.Instance.Conventions.UnifiedSearchRegistry
                .ForInstanceOf<SitePageData>()
                .CustomizeProjection(
                    x => x.ProjectImageUriFrom<SitePageData>(
                        page => GetPageImage(page.PageImage.OriginalString)));

Customize the default projection of files when using Unified Search to include an image. As there is no image associated with individual files we retrieve the file extension from the index and project in known URLs for icons.

            SearchClient.Instance.Conventions.UnifiedSearchRegistry
                .ForInstanceOf<UnifiedFile>()
                .CustomizeProjection(
                    x => x.ProjectImageUriFrom<UnifiedFile>(
                        file => GetFileImage(file.Extension)));
        }

Utility method used when projecting images for pages. While it would be possible to include this in the projection expression it would have looked nasty.

        private static Uri GetPageImage(string url)
        {
            if(string.IsNullOrWhiteSpace(url))
            {
                return null;
            }

            return new Uri(url, UriKind.Relative);
        }

Utility method used when projecting images/icons for files. While it would be possible to include this in the projection expression it would have looked nasty.

        private static Uri GetFileImage(string extension)
        {
            if(extension == ".pdf")
            {
                return new Uri("/static/gfx/file_pdf.png", UriKind.Relative);
            }
            if (extension == ".doc" || extension == ".docx")
            {
                return new Uri("/static/gfx/file_word.png", UriKind.Relative);
            }
            return null;
        }

        public void Uninitialize(InitializationEngine context)
        {
        }

        public void Preload(string[] parameters)
        {
        }
    }
}