Last active
February 28, 2016 22:17
-
-
Save RyanABailey/eecf7fab8df39252bee5 to your computer and use it in GitHub Desktop.
Sitecore custom index field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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