A couple Sitecore 7 computed fields
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Sitecore.ContentSearch; | |
using Sitecore.ContentSearch.ComputedFields; | |
using Sitecore.ContentSearch.Utilities; | |
using Sitecore.Data.Items; | |
namespace ActiveCommerce.Search.ComputedFields | |
{ | |
public class AllTemplatesField : IComputedIndexField | |
{ | |
public string FieldName { get; set; } | |
public string ReturnType { get; set; } | |
public object ComputeFieldValue(IIndexable indexable) | |
{ | |
var indexItem = indexable as SitecoreIndexableItem; | |
var item = (Sitecore.Data.Items.Item)indexItem.Item; | |
var templates = new List<string>(); | |
this.GetAllTemplates(item.Template, templates); | |
return templates; | |
} | |
public void GetAllTemplates(TemplateItem baseTemplate, List<string> list) | |
{ | |
if (baseTemplate.ID != Sitecore.TemplateIDs.StandardTemplate) | |
{ | |
string str = IdHelper.NormalizeGuid(baseTemplate.ID); | |
list.Add(str); | |
foreach (TemplateItem item in baseTemplate.BaseTemplates) | |
{ | |
this.GetAllTemplates(item, list); | |
} | |
} | |
} | |
} | |
} |
This file contains 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
using Sitecore; | |
using Sitecore.ContentSearch; | |
using Sitecore.Data; | |
using Sitecore.Data.Items; | |
using Sitecore.Diagnostics; | |
using Sitecore.Links; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Sitecore.ContentSearch.Pipelines.GetDependencies; | |
namespace ActiveCommerce.Search.GetDependencies | |
{ | |
public class GetDatasourceDependencies : BaseProcessor | |
{ | |
public override void Process(GetDependenciesArgs context) | |
{ | |
Func<ItemUri, bool> func = null; | |
Assert.IsNotNull(context.IndexedItem, "indexed item"); | |
Assert.IsNotNull(context.Dependencies, "dependencies"); | |
Item item = (Item)(context.IndexedItem as SitecoreIndexableItem); | |
if (item != null) | |
{ | |
if (func == null) | |
{ | |
func = uri => (bool)((uri != null) && ((bool)(uri != item.Uri))); | |
} | |
System.Collections.Generic.IEnumerable<ItemUri> source = Enumerable.Where<ItemUri>(from l in Globals.LinkDatabase.GetReferrers(item, FieldIDs.LayoutField) select l.GetSourceItem().Uri, func).Distinct<ItemUri>(); | |
context.Dependencies.AddRange(source.Select(x => (SitecoreItemUniqueId)x)); | |
} | |
} | |
} | |
} |
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Sitecore; | |
using Sitecore.ContentSearch; | |
using Sitecore.ContentSearch.ComputedFields; | |
using Sitecore.Data.Fields; | |
using Sitecore.Data.Items; | |
using Sitecore.Diagnostics; | |
using Sitecore.Links; | |
namespace ActiveCommerce.Search.ComputedFields | |
{ | |
/// <summary> | |
/// Crawls the renderings on an item and includes their content in the index | |
/// </summary> | |
public class VisualizationField : IComputedIndexField | |
{ | |
private readonly HashSet<string> _textFieldTypes = new HashSet<string>(new[] | |
{ | |
"Single-Line Text", | |
"Rich Text", | |
"Multi-Line Text", | |
"text", | |
"rich text", | |
"html", | |
"memo", | |
"Word Document" | |
}); | |
public string FieldName { get; set; } | |
public string ReturnType { get; set; } | |
public object ComputeFieldValue(IIndexable indexable) | |
{ | |
var item = (Item)(indexable as SitecoreIndexableItem); | |
Assert.ArgumentNotNull(item, "item"); | |
if (!ShouldIndexItem(item)) | |
{ | |
return null; | |
} | |
IEnumerable<Item> dataSources = | |
Globals.LinkDatabase.GetReferences(item) | |
.Where(link => ShouldProcessLink(link, item)) | |
.Select(link => link.GetTargetItem()) | |
.Where(targetItem => targetItem != null) | |
.Distinct(); | |
var result = new StringBuilder(); | |
foreach (var dataSource in dataSources.Where(ShouldIndexDataSource)) | |
{ | |
dataSource.Fields.ReadAll(); | |
foreach (var field in dataSource.Fields.Where(ShouldIndexField)) | |
{ | |
result.AppendLine(field.Value); | |
} | |
} | |
return result.ToString(); | |
} | |
protected virtual bool ShouldIndexItem(Item item) | |
{ | |
//only items w/ layout that are not template standard values | |
return item.Visualization != null && item.Visualization.Layout != null && !item.Paths.LongID.Contains(ItemIDs.TemplateRoot.ToString()); | |
} | |
protected virtual bool ShouldProcessLink(ItemLink link, Item sourceItem) | |
{ | |
//layout field references in the same database | |
return link.SourceFieldID == FieldIDs.LayoutField && link.SourceDatabaseName == sourceItem.Database.Name; | |
} | |
protected virtual bool ShouldIndexDataSource(Item item) | |
{ | |
//don't process references to renderings | |
return !item.Paths.LongID.Contains(ItemIDs.LayoutRoot.ToString()); | |
} | |
protected virtual bool ShouldIndexField(Field field) | |
{ | |
//process non-empty text fields that are not part of the standard template | |
return !field.Name.StartsWith("__") && IsTextField(field) && !string.IsNullOrEmpty(field.Value); | |
} | |
protected virtual bool IsTextField(Field field) | |
{ | |
return _textFieldTypes.Contains(field.Type); | |
} | |
} | |
} |
This file contains 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
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> | |
<sitecore> | |
<contentSearch> | |
<configuration type="Sitecore.ContentSearch.LuceneProvider.LuceneSearchConfiguration, Sitecore.ContentSearch.LuceneProvider"> | |
<defaultIndexConfiguration> | |
<fields hint="raw:AddComputedIndexField"> | |
<field fieldName="_templates" storageType="no" indexType="untokenized">ActiveCommerce.Search.ComputedFields.AllTemplatesField, ActiveCommerce.Kernel</field> | |
<field fieldName="_content" storageType="no" indexType="tokenized">ActiveCommerce.Search.ComputedFields.VisualizationField, ActiveCommerce.Kernel</field> | |
</fields> | |
</defaultIndexConfiguration> | |
</configuration> | |
</contentSearch> | |
<pipelines> | |
<indexing.getDependencies help="Processors should derive from Sitecore.ContentSearch.Pipelines.GetDependencies.BaseProcessor"> | |
<!-- When indexing an item, make sure any items for whom it is a datasource get re-indexed as well --> | |
<processor type="ActiveCommerce.Search.GetDependencies.GetDatasourceDependencies, ActiveCommerce.Kernel"/> | |
</indexing.getDependencies> | |
</pipelines> | |
</sitecore> | |
</configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment