Skip to content

Instantly share code, notes, and snippets.

@RyanABailey
Last active February 28, 2016 22:17
Show Gist options
  • Select an option

  • Save RyanABailey/eecf7fab8df39252bee5 to your computer and use it in GitHub Desktop.

Select an option

Save RyanABailey/eecf7fab8df39252bee5 to your computer and use it in GitHub Desktop.
Sitecore custom index field
namespace MyNamespace
{
public class IndexChildContent : IComputedIndexField
{
/// <inheritdoc />
public string FieldName { get; set; }
/// <inheritdoc />
public string ReturnType { get; set; }
/// <inheritdoc />
public object ComputeFieldValue(IIndexable indexable)
{
Item item = indexable as SitecoreIndexableItem;
if (item != null)
{
try
{
var indexedContent = string.Empty;
var components = item.Children.Where(x => x.TemplateID == new Sitecore.Data.ID("{7D396D96-54D5-4F2A-B552-4177F1831D41}")).FirstOrDefault(); // datasource template
if (components != null)
{
var dataitems = components.Children.Where(x => x.TemplateID == new Sitecore.Data.ID("{70BB078F-C283-40F3-AD63-962780070879}") || x.TemplateID == new Sitecore.Data.ID("{64E5D773-A6CB-46F6-84FE-93FE2C9A79CB}")); // Rich text template types
foreach (var content in dataitems)
{
var indexField = content.Fields["Text"];
if (indexField.HasValue)
{
indexedContent = indexedContent + Sitecore.StringUtil.RemoveTags(indexField.Value);
}
}
return indexedContent;
}
}
catch (Exception)
{
return null;
}
}
return null; // Return null if nothing to index
}
}
}
<fields hint="raw:AddComputedIndexField">
<field fieldName="childcontent" storageType="yes" indexType="untokenized"
patch:after="field[last()]">MyNamespace.IndexChildContent, MyNamespace</field>
</fields>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment