1

Is there a way to re-use the code for a computed index field without just making derived classes? I would like to have an additional property in the config which tells the ComputeFieldValue method which template field to read from.

So my config patch would look like this:

<field 
  fieldName="mounting"
  templateFieldName="Mounting" <<new property here
  returnType="stringCollection">
    MyNamespace.MyComputedIndexField, MyAssembly
</field>

And my indexing class would be as follows:

public class MyComputedIndexField : AbstractComputedIndexField
{
    public string TemplateFieldName { get; set; }

    public override object ComputeFieldValue(IIndexable indexable)
    {
        Item item = indexable as SitecoreIndexableItem;
        if (item == null)
        {
            return null;
        }

        var values = item[TemplateFieldName]?.Split(new[] {";"}, StringSplitOptions.RemoveEmptyEntries); //readthe TemplateFieldName property here
        return values?.Select(v => v.Trim()).ToList();
    }
}

Obviously, I could just set the default fieldName property to the name of the template field, but that would constrain my field name to be named after the template field name.

4

Yes, you can extract custom parameters from XmlNode of your field configuration:

<field 
  fieldName="mounting"
  templateFieldName="Mounting" 
  returnType="stringCollection">
    MyNamespace.MyComputedIndexField, MyAssembly
</field>
using System.Xml;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.ComputedFields;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Xml;

public class MyComputedIndexField : IComputedIndexField 
{
    public string FieldName { get; set; }
    public string ReturnType { get; set; }
    public string TemplateFieldName { get; set; }

    public MyComputedIndexField(XmlNode configNode)
    {
        this.FieldName = XmlUtil.GetAttribute("fieldName", configNode);
        this.ReturnType = XmlUtil.GetAttribute("returnType", configNode);
        this.TemplateFieldName = XmlUtil.GetAttribute("templateFieldName", configNode);            
    }
        
    public virtual object ComputeFieldValue(IIndexable indexable)
    {
        Item item = indexable as SitecoreIndexableItem;
        if (item == null)
        {
            return null;
        }

        var values = item[TemplateFieldName]?.Split(new[] {";"}, StringSplitOptions.RemoveEmptyEntries); //readthe TemplateFieldName property here
        return values?.Select(v => v.Trim()).ToList();
    }
}

or with inheritance from AbstractComputedIndexField:

public class MyComputedIndexField : AbstractComputedIndexField
{
    public string TemplateFieldName { get; set; }

    public MyComputedIndexField(XmlNode configNode) : base(configNode)
    {
        this.TemplateFieldName = XmlUtil.GetAttribute("templateFieldName", configNode);            
    }
        
    public override object ComputeFieldValue(IIndexable indexable)
    {
        Item item = indexable as SitecoreIndexableItem;
        if (item == null)
        {
            return null;
        }

        var values = item[TemplateFieldName]?.Split(new[] {";"}, StringSplitOptions.RemoveEmptyEntries); //readthe TemplateFieldName property here
        return values?.Select(v => v.Trim()).ToList();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.