Skip to content

Instantly share code, notes, and snippets.

@abjerner
Created December 23, 2014 14:42
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abjerner/bdd89e0788d274ec5a33 to your computer and use it in GitHub Desktop.
Save abjerner/bdd89e0788d274ec5a33 to your computer and use it in GitHub Desktop.

By using my Skybrud.Umbraco.GridData package (which introduces a strongly typed model for the Grid), indexing the new Grid in Umbraco 7.2 is quite easy.

At Skybrud.dk we typically create a class named ExamineIndexer that takes care of the Examine related events. This class should be initalized during Umbraco startup like this:

using Umbraco.Core;

namespace FanoeTest {

    public class Startup : ApplicationEventHandler {

        private static ExamineIndexer _examineIndexer;

        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) {
            // Register events for Examine
            _examineIndexer = new ExamineIndexer();
        }

    }

}

The ExamineIndexer class it self will now look like the example below. The example is very specific, since only certain document types where we know that the content property holds a Grid value are handled.

The example could be modified to use the Content Service for finding all properties that holds a Grid value. But since the Content Service uses the database, it may slow the performance when building your index.

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using Examine;
using Examine.Providers;
using Skybrud.Umbraco.GridData;
using Skybrud.Umbraco.GridData.Values;
using Umbraco.Core.Logging;

namespace FanoeTest {
   
    public class ExamineIndexer {

        public ExamineIndexer() {

            BaseIndexProvider externalIndexer = ExamineManager.Instance.IndexProviderCollection["ExternalIndexer"];

            externalIndexer.GatheringNodeData += OnExamineGatheringNodeData;

        }

        private void OnExamineGatheringNodeData(object sender, IndexingNodeDataEventArgs e) {

            try {

                string nodeTypeAlias = e.Fields["nodeTypeAlias"];

                LogHelper.Info<ExamineIndexer>("Gathering node data for node #" + e.NodeId + " (type: " + nodeTypeAlias + ")");

                if (nodeTypeAlias == "Home" || nodeTypeAlias == "LandingPage" || nodeTypeAlias == "TextPage" || nodeTypeAlias == "BlogPost") {

                    string value;

                    if (e.Fields.TryGetValue("content", out value)) {

                        LogHelper.Info<ExamineIndexer>("Node has \"content\" value\"");

                        GridDataModel grid = GridDataModel.Deserialize(e.Fields["content"]);

                        StringBuilder combined = new StringBuilder();

                        foreach (GridControl ctrl in grid.GetAllControls()) {

                            switch (ctrl.Editor.Alias) {

                                case "rte": {

                                    // Get the HTML value
                                    string html = ctrl.GetValue<GridControlRichTextValue>().Value;

                                    // Strip any HTML tags so we only have text
                                    string text = Regex.Replace(html, "<.*?>", "");

                                    // Extra decoding may be necessary
                                    text = HttpUtility.HtmlDecode(text);

                                    // Now append the text
                                    combined.AppendLine(text);
                                    
                                    break;
                                
                                }
                                
                                case "media": {
                                    GridControlMediaValue media = ctrl.GetValue<GridControlMediaValue>();
                                    combined.AppendLine(media.Caption);
                                    break;
                                }

                                case "headline":
                                case "quote": {
                                    combined.AppendLine(ctrl.GetValue<GridControlTextValue>().Value);
                                    break;
                                }

                            }

                        }

                        e.Fields["content"] = combined.ToString();

                    } else {

                        LogHelper.Info<ExamineIndexer>("Node has no \"content\" value\"");

                    }

                }

            } catch (Exception ex) {

                LogHelper.Error<ExamineIndexer>("MAYDAY! MAYDAY! MAYDAY!", ex);
            
            }

        }

    }

}
@slseverance
Copy link

I'm having an issue with the basic installation. I read the instruction on the grid.skybrud.dk site that says simply drop the dlls in the bin folder. So I have downloaded the ZIP and pulled the xmls and dlls out of the latest release folder and dropped them into the bin folder. I restarted the app pool. It's not working so I assume the installation is more complicated than that? I have built a template using your example code and my aliases so i feel confident I have simply not installed it correctly. Must this be built and recompiled before operation or will a simple restart of service pick up the new packages? I don't have a viz studio solution set up. Thank you.

@zipswich
Copy link

Thank you for the example. I have largely copied the code, and made sure ExamineIndexer() is called. Unfortunately, OnExamineGatheringNodeData is never invoked. Could you provide a tip on how to debug this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment