Skip to content

Instantly share code, notes, and snippets.

@Azadehkhojandi
Created February 5, 2016 05:20
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 Azadehkhojandi/c79dcb009f38acf26241 to your computer and use it in GitHub Desktop.
Save Azadehkhojandi/c79dcb009f38acf26241 to your computer and use it in GitHub Desktop.
//references
//http://www.sitecore.net/Learn/Blogs/Technical-Blogs/John-West-Sitecore-Blog/Posts/2013/05/Sitecore-7-Enable-Default-Computed-Index-Fields.aspx
//http://kamsar.net/index.php/2014/05/indexing-subcontent/
//http://www.techphoria414.com/Blog/2013/November/Sitecore-7-Computed-Fields-All-Templates-and-Datasource-Content
public class SubcontentField : IComputedIndexField
{
public string FieldName { get; set; }
public string ReturnType { get; set; }
public object ComputeFieldValue(IIndexable indexable)
{
try
{
var sitecoreIndexable = indexable as SitecoreIndexableItem;
if (sitecoreIndexable == null)
{
return null;
}
// find renderings with datasources set
var customDataSources = ExtractRenderingDataSourceItems(sitecoreIndexable.Item);
// extract text from data sources
var contentToAdd = customDataSources.SelectMany(GetItemContent).ToList();
if (contentToAdd.Count == 0)
{
return null;
}
return string.Join(" ", contentToAdd);
}
catch (Exception ex)
{
Log.Error($"An error occurred when indexing {indexable.Id}: {ex.Message}", ex, this);
}
return null;
}
/// <summary>
/// Finds all renderings on an item's layout details with valid custom data sources set and returns the data source items.
/// </summary>
protected virtual IEnumerable<Item> ExtractRenderingDataSourceItems(Item baseItem)
{
//azadeh test
//if (baseItem.ID == ID.Parse("{6A139D21-0EBB-469C-94A8-FC1C40E45EB9}"))
//{
// Debugger.Break();
//}
//rendering
var currentLayoutXml = LayoutField.GetFieldValue(baseItem.Fields[FieldIDs.LayoutField]);
//final rendering
var finalLayoutXml = LayoutField.GetFieldValue(baseItem.Fields[FieldIDs.FinalLayoutField]);
var layoutXml = !string.IsNullOrEmpty(finalLayoutXml) ? finalLayoutXml: currentLayoutXml;
if (string.IsNullOrEmpty(layoutXml))
{
yield break;
}
var layout = LayoutDefinition.Parse(layoutXml);
// loop over devices in the rendering
for (var deviceIndex = layout.Devices.Count - 1; deviceIndex >= 0; deviceIndex--)
{
var device = layout.Devices[deviceIndex] as DeviceDefinition;
if (device == null)
{
continue;
}
// loop over renderings within the device
for (var renderingIndex = device.Renderings.Count - 1; renderingIndex >= 0; renderingIndex--)
{
var rendering = device.Renderings[renderingIndex] as RenderingDefinition;
if (rendering == null)
{
continue;
}
// if the rendering has a custom data source, we resolve the data source item and place its text fields into the content to add
if (!string.IsNullOrWhiteSpace(rendering.Datasource))
{
//TODO:[Aza] - investigate on it - DataSourceHelper is a component of Blade
//var dataSource = DataSourceHelper.ResolveDataSource(rendering.Datasource, baseItem);
var dataSource = baseItem.Database.GetItem(rendering.Datasource);
if (dataSource != baseItem)
{
// Return the parent before its children
yield return dataSource;
if (dataSource.HasChildren) //TODO:[Aza] better logic check if datasource inherited from folder template
{
foreach (Item item in dataSource.GetChildren())
{
yield return item;
}
}
}
}
}
}
}
/// <summary>
/// Extracts textual content from an item's fields
/// </summary>
protected virtual IEnumerable<string> GetItemContent(Item dataSource)
{
foreach (Field field in dataSource.Fields)
{
// this check is what Sitecore uses to determine if a field belongs in _content (see LuceneDocumentBuilder.AddField())
if (!IndexOperationsHelper.IsTextField(new SitecoreItemDataField(field)))
{
continue;
}
var fieldValue =(field.Value ?? string.Empty).StripTags();
if (!string.IsNullOrWhiteSpace(fieldValue))
{
yield return fieldValue;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment