Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. using Colossus.Sc.Foundation.SitecoreExtensions.Extensions;
  2. using Sitecore.ContentSearch;
  3. using Sitecore.ContentSearch.ComputedFields;
  4. using Sitecore.Data;
  5. using Sitecore.Data.Fields;
  6. using Sitecore.Data.Items;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Web;
  13.  
  14. namespace Colossus.Sc.Foundation.Search.Extensions
  15. {
  16. public class ContentField : AbstractComputedIndexField
  17. {
  18. public override object ComputeFieldValue(IIndexable indexable)
  19. {
  20. Item item = indexable as SitecoreIndexableItem;
  21. if (item == null) return null;
  22. StringBuilder sbData = new StringBuilder();
  23. var renderings = GetRenderingReferences(item, "default");
  24. if (renderings == null)
  25. return null;
  26. if (item.Versions.IsLatestVersion())
  27. {
  28. foreach (var rendering in renderings.Where(x=>x.RenderingID == new ID(Templates.RichTextRenderingId)))
  29. {
  30. if (string.IsNullOrEmpty(rendering.Settings.DataSource)) continue;
  31. var dataSourceItem = item.Database.GetItem(rendering.Settings.DataSource);
  32. //the fields of the rendering of the current item will be added to index
  33. if (dataSourceItem != null)
  34. {
  35. foreach (Field field in dataSourceItem.Fields)
  36. {
  37. if (!string.IsNullOrWhiteSpace(field.Value))
  38. {
  39. sbData.AppendFormat("{0} ", StripHtml(field.Value));
  40. }
  41. }
  42. }
  43. }
  44. }
  45. return sbData.ToString().Trim();
  46. }
  47.  
  48. private Sitecore.Layouts.RenderingReference[] GetRenderingReferences(Item item, string deviceName)
  49. {
  50. LayoutField layoutField = item.Fields["__final renderings"];
  51. if (layoutField == null)
  52. return null;
  53. Sitecore.Layouts.RenderingReference[] renderings = null;
  54. if (item.Database != null)
  55. {
  56. renderings = layoutField.GetReferences(GetDeviceItem(item.Database, deviceName));
  57. }
  58. else
  59. {
  60. renderings = layoutField.GetReferences(GetDeviceItem(Sitecore.Context.Database, deviceName));
  61. }
  62. return renderings;
  63. }
  64.  
  65. private DeviceItem GetDeviceItem(Sitecore.Data.Database db, string deviceName)
  66. {
  67. return db.Resources.Devices.GetAll().Where(d => d.Name.ToLower() == deviceName.ToLower()).First();
  68. }
  69.  
  70. public static string StripHtml(string source)
  71. {
  72. var htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);
  73. var removedTags = htmlRegex.Replace(source, string.Empty);
  74. return HttpUtility.HtmlDecode(removedTags);
  75. }
  76.  
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement