London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

Using FIND to index contents of a block added to page as a ContentReference

Vote:
 

Episerver version 11.12

We are using find to retrieve some content based on search text.  I can index content blocks on my page that are added to a Content Area if they are marked with the [IndexInContentAreas] attribute, but it doesn't seem to index content blocks that are added as ContentReference type (see sample below).  These blocks are also decorated with the [IndexInContentAreas] attribute but don't return results.

How do I get FIND to index the contents of the block in a ContentReference?

[Required]
[Display(
    Name = "Fund Profile",
    Description = "Content block with fund details",
    Order = 10)]
[AllowedTypes(typeof(FundTemplateBlock))]
public virtual ContentReference FundTemplate { get; set; }
#225842
Jul 28, 2020 16:13
Vote:
 

I guess you would need to add an extension method for your content, something like

public static FundTemplateBlock FundBlock(this MyContent content)

{

//Load and return your block

}

and include this in your convention

#225869
Jul 29, 2020 5:25
Vote:
 

Hi Eric,

For that, you need to do some level of customization. Create an IniitializationModule and add you custom property to index like this

[InitializableModule]
[ModuleDependency(typeof(IndexingModule))]
public class SearchInitialization : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        SearchClient.Instance.Conventions
            .ForInstancesOf<MyPage>()
            .IncludeField(x => x.FundTemplateContent());
    }
}

Then, in your method, FundTemplateContent() get your block from ContentReference property and return content to index it. (see below example)

public static string FundTemplateContent(this MyPage page)
{
    var fundTemplate = string.Empty;
    if (page?.FundTemplate == null || ContentReference.IsNullOrEmpty(page.FundTemplate))
        return fundTemplate;

    var content = ContentLoader.Service.Get<FundTemplateBlock>(page.FundTemplate);
    if (content != null)
    {
        fundTemplate = content.Property1; //string property
        fundTemplate += content.Property2; //string property
    }
    return fundTemplate;
}

Note: Above code is not completely tested. This is a sample code :)

Hope this will help.

#225871
Jul 29, 2020 7:29
Vote:
 

This worked.  Thanks.  Made a couple of small changes to get it working.   See working code below.

    [InitializableModule]
    [ModuleDependency(typeof(IndexingModule))]
    public class SearchInitialization : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            SearchClient.Instance.Conventions
                .ForInstancesOf<FundDetailsPage>()
                .IncludeField(x => x.FundTemplateContent(x));
        }

        public void Uninitialize(InitializationEngine context)
        {}
    }

And my FundTemplateContent method inside my FundDetailsPage

  public string FundTemplateContent(FundDetailsPage page)
  {
      var fundTemplate = string.Empty;
      if (page?.FundTemplate == null || 
          ContentReference.IsNullOrEmpty(page.FundTemplate))
          { return fundTemplate; }

      var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
      var content = contentLoader.Get<FundTemplateBlock>(page.FundTemplate);

      if (content != null)
      {
          fundTemplate = content. ///string field;
          ... //all other string fields
      }

      return fundTemplate;
  }
#225884
Jul 29, 2020 18:38
Praful Jangid - Jul 29, 2020 18:43
Good Job :)
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.