Associating documents/PDF to a page/pagetype in Episerver FIND

I've seen different solutions out there, but sometimes the one with less code is also the simplest and best.

icon of user profile

Published 9th of November 2018
Episerver Find 13

Scenario

You want to get a search hit from an associated document when you make a free-text search on a typed object or with unified search.

Prerequisites

My DataModel looks like this:

    public class MyPageDataModel: PageData
    {
        [Display(
            Name = "Dokument",
            Description = "Dokument för publikation",
            GroupName = SystemTabNames.Content,
            Order = 1)]
        [UIHint(UIHint.MediaFile)]
        [Required]
        public virtual Url MyLinkedItem { get; set; }

     }

Find Initialization config

SearchClient.Instance.Conventions.ForInstancesOf<MyPageDataModel>().IncludeField(x => x.IndexAttachment());

public static Attachment IndexAttachment(this MyPageDataModel myPage)
{
    if (UrlResolver.Current.Route(new UrlBuilder(myPage.MyLinkedItem)) is MediaData media)
    {
        MemoryStream io = new MemoryStream((media?.BinaryData as FileBlob).ReadAllBytes());
        return new Attachment(() => io);
    }
    return new Attachment(() => null);
}

The query:

var result = client.Search<MyPageDataModel>().For("Banana").GetResult();
//Your pdf contains banana of course

With this easy approach, it will work, even though you won’t be able to see the connection in Find > Overview > Explore.

Options

Cant find Documents in Episerver FIND UI?

If you install “EPiServer.Find.Cms.AttachmentFilter“, this makes “documents/genericmedia” appear in Find > Overview > Explore

Further Options and reading

SEO terms

    • Index documents to a page in Episerver FIND
    • Searching for pages based on free-text content of associated MediaData
    • Filter file used on child pages

About the author

Luc Gosso
– Independent Senior Web Developer
working with Azure and Episerver

Twitter: @LucGosso
LinkedIn: linkedin.com/in/luc-gosso/
Github: github.com/lucgosso