Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Colossus.Sc.Foundation.SitecoreExtensions.Extensions;
- using Sitecore.ContentSearch;
- using Sitecore.ContentSearch.ComputedFields;
- using Sitecore.Data;
- using Sitecore.Data.Fields;
- using Sitecore.Data.Items;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Web;
- namespace Colossus.Sc.Foundation.Search.Extensions
- {
- public class ContentField : AbstractComputedIndexField
- {
- public override object ComputeFieldValue(IIndexable indexable)
- {
- Item item = indexable as SitecoreIndexableItem;
- if (item == null) return null;
- StringBuilder sbData = new StringBuilder();
- var renderings = GetRenderingReferences(item, "default");
- if (renderings == null)
- return null;
- if (item.Versions.IsLatestVersion())
- {
- foreach (var rendering in renderings.Where(x=>x.RenderingID == new ID(Templates.RichTextRenderingId)))
- {
- if (string.IsNullOrEmpty(rendering.Settings.DataSource)) continue;
- var dataSourceItem = item.Database.GetItem(rendering.Settings.DataSource);
- //the fields of the rendering of the current item will be added to index
- if (dataSourceItem != null)
- {
- foreach (Field field in dataSourceItem.Fields)
- {
- if (!string.IsNullOrWhiteSpace(field.Value))
- {
- sbData.AppendFormat("{0} ", StripHtml(field.Value));
- }
- }
- }
- }
- }
- return sbData.ToString().Trim();
- }
- private Sitecore.Layouts.RenderingReference[] GetRenderingReferences(Item item, string deviceName)
- {
- LayoutField layoutField = item.Fields["__final renderings"];
- if (layoutField == null)
- return null;
- Sitecore.Layouts.RenderingReference[] renderings = null;
- if (item.Database != null)
- {
- renderings = layoutField.GetReferences(GetDeviceItem(item.Database, deviceName));
- }
- else
- {
- renderings = layoutField.GetReferences(GetDeviceItem(Sitecore.Context.Database, deviceName));
- }
- return renderings;
- }
- private DeviceItem GetDeviceItem(Sitecore.Data.Database db, string deviceName)
- {
- return db.Resources.Devices.GetAll().Where(d => d.Name.ToLower() == deviceName.ToLower()).First();
- }
- public static string StripHtml(string source)
- {
- var htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);
- var removedTags = htmlRegex.Replace(source, string.Empty);
- return HttpUtility.HtmlDecode(removedTags);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement