Skip to content

Instantly share code, notes, and snippets.

@bala-one
Created March 27, 2025 01:30
Show Gist options
  • Save bala-one/9301b55710349dc126356f95d4e6389f to your computer and use it in GitHub Desktop.
Save bala-one/9301b55710349dc126356f95d4e6389f to your computer and use it in GitHub Desktop.
Coveo Custom Computed Field
using System;
using Sitecore.ContentSearch;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.ContentSearch.Diagnostics;
using Sitecore.ContentSearch.ComputedFields;
namespace Balaone.Coveo {
public abstract class BaseCoveoComputedField : IComputedIndexField
{
public string FieldName { get; set; }
public string ReturnType { get; set; }
public abstract Object ComputeFieldValue(IIndexable indexable);
protected Item GetItemFromIndexable(IIndexable indexable)
{
try
{
string typeName = GetType().Name;
if (indexable == null || !(indexable is SitecoreIndexableItem sitecoreIndexable))
{
CrawlingLog.Log.Warn($"Indexable is null or non indexable for field {typeName}");
}
else if (indexable != null)
{
return sitecoreIndexable.Item;
}
}
catch (Exception ex)
{
CrawlingLog.Log.Error($"Error in GetItemFromIndexable method for field: {typeName}", ex);
}
return null;
}
}
public class MyComputedField : BaseCoveoComputedField
{
public override object ComputeFieldValue(IIndexable indexable)
{
var item = GetItemFromIndexable(indexable);
var isInherited = item?.InheritsTemplate("YOUR TEMPLATE GUID");
if (item == null || !isInherited)
{
return null;
}
TextField field = item.Fields["MyFieldName"];
if (field == null || string.IsNullOrEmpty(field.Value))
{
return null;
}
return field.Value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment