Skip to content

Instantly share code, notes, and snippets.

@KKings
Created June 12, 2015 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KKings/19794f34fb6f35f39a9d to your computer and use it in GitHub Desktop.
Save KKings/19794f34fb6f35f39a9d to your computer and use it in GitHub Desktop.
Sitecore 8 replacement for raw:AddCustomField from Sitecore 7
namespace NavArts.Sitecore.Search.ComputedFields
{
using System;
using System.Linq;
using System.Xml;
using global::Sitecore.ContentSearch;
using global::Sitecore.ContentSearch.ComputedFields;
using global::Sitecore.Data;
using global::Sitecore.Data.Fields;
using global::Sitecore.Diagnostics;
/// <summary>
/// Adds a Sitecore Field to the Index.
/// </summary>
public sealed class CustomIndexField : IComputedIndexField
{
/// <summary>
/// Gets or sets the FieldName from the Index Configuration File
/// </summary>
public string FieldName { get; set; }
/// <summary>
/// Gets or sets the ReturnType from the Index Configuration File
/// </summary>
public string ReturnType { get; set; }
/// <summary>
/// Gets or sets the SitecoreFieldName
/// <para>Controlls what field to pull data out of</para>
/// </summary>
public string SitecoreFieldName { get; set; }
/// <summary>
/// Creates a new instance of the CustomIndexField with a null XmlNode
/// </summary>
public CustomIndexField() : this((XmlNode)null)
{
}
/// <summary>
/// Creates a new instance of the CustomIndexField with the passed in configuration
/// </summary>
public CustomIndexField(XmlNode configurationNode)
{
this.Initialize(configurationNode);
}
/// <summary>
/// Check to see if the Indexed Item is a Standard Values Item
/// </summary>
/// <param name="indexable">The Indexed Item</param>
/// <returns>True if the Item being indexed is a Standard Values Item</returns>
public object ComputeFieldValue(IIndexable indexable)
{
var item = (SitecoreIndexableItem)indexable;
if (item != null
&& item.Item != null
&& !String.IsNullOrEmpty(this.SitecoreFieldName))
{
var field = FieldTypeManager.GetField(item.Item.Fields[this.SitecoreFieldName]);
if (field != null && field.InnerField != null)
{
var multilistField = field as MultilistField;
if (multilistField != null)
{
if (multilistField.TargetIDs.Any())
{
return multilistField.TargetIDs.Select(id => ShortID.Encode(id).ToLower());
}
}
var lookupField = field as LookupField;
if (lookupField != null)
{
if (lookupField.TargetItem != null)
{
return ShortID.Encode(lookupField.TargetID).ToLower();
}
}
if (field.InnerField.HasValue)
{
return field.InnerField.Value;
}
}
}
return null;
}
/// <summary>
/// Sets the <see cref="SitecoreFieldName"/> from the configuration elements
/// </summary>
/// <param name="configurationNode">Passed in configuration</param>
private void Initialize(XmlNode configurationNode)
{
if (configurationNode == null)
{
Log.Error("<name> configuration error: cannot be found.", this);
return;
}
if (configurationNode.ChildNodes.Count > 0)
{
var xmlNode = configurationNode.SelectSingleNode("name");
if (xmlNode == null || String.IsNullOrEmpty(xmlNode.InnerText))
{
Log.Error("<name> configuration error: cannot be empty.", this);
return;
}
this.SitecoreFieldName = xmlNode.InnerText;
}
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<contentSearch>
<indexConfigurations>
<defaultLuceneIndexConfiguration>
<!-- Examples:
Adding multiple fields of the same name to the same document
The <name> sets the Sitecore field name of the item to set
-->
<fields hint="raw:AddComputedIndexField">
<field fieldName="tags" type="NavArts.Sitecore.Search.ComputedFields.CustomIndexField,NavArts.Sitecore">
<name>tags</name>
</field>
<field fieldName="tags" type="NavArts.Sitecore.Search.ComputedFields.CustomIndexField,NavArts.Sitecore">
<name>topics</name>
</field>
</fields>
<!-- Examples:
Map our 'tags' computed fields to an index field
-->
<fieldMap type="Sitecore.ContentSearch.FieldMap, Sitecore.ContentSearch">
<fieldNames hint="raw:AddFieldByFieldName">
<field fieldName="tags" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider">
<Analyzer type="Sitecore.ContentSearch.LuceneProvider.Analyzers.LowerCaseKeywordAnalyzer, Sitecore.ContentSearch.LuceneProvider" />
</field>
</fieldNames>
</fieldMap>
</defaultLuceneIndexConfiguration>
</indexConfigurations>
</contentSearch>
</sitecore>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment