Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jason Espin 368 posts 1335 karma points
    Aug 26, 2015 @ 08:21
    Jason Espin
    0

    Storing a custom field in the Examine index on page save event

    Hi all,

    I have recently upgraded to Umbraco 7.2.8 from 7.2.4 and it would seem that in this version everything has changed when it comes to writing custom fields to the Examine index in Umbraco.

    Previously if I wanted to write a custom field to the Examine index I would do the following:

    public class ExamineEvents : ApplicationBase
        {
            public ExamineEvents()
            {
                ExamineManager.Instance.IndexProviderCollection["ExternalIndexer"].GatheringNodeData += ExamineEvents_GatheringNodeData;
                ExamineManager.Instance.IndexProviderCollection["AxumIndexer"].GatheringNodeData += ExamineEvents_GatheringNodeData;
                ExamineManager.Instance.IndexProviderCollection["ToursIndexer"].GatheringNodeData += ExamineEvents_GatheringNodeData;
            }
    
    
            void ExamineEvents_GatheringNodeData(object sender, IndexingNodeDataEventArgs e)
            {
                AddToContentsField(e);
            }
    
            private void AddToContentsField(IndexingNodeDataEventArgs e)
            {
                Dictionary<string,string> fields = e.Fields;
                string level = fields["level"];
                string id = fields["id"];
    
                if (level != "1")
                {
                    string nodeTypeAlias = fields["nodeTypeAlias"];
                    int nodeId = 0;
                    int ancestorId = 0;
                    try
                    {
                        nodeId = Convert.ToInt32(id);
                        if (nodeId != 0)
                        {
                            IContentService cs = ApplicationContext.Current.Services.ContentService;
                            try
                            {
                                IEnumerable<IContent> ancestors = cs.GetAncestors(nodeId);
                                if (ancestors.Where(x => x.ContentType.Alias.ToLower() == "home").Count() == 1)
                                {
                                    ancestorId = ancestors.First().Id;
                                }
                                if (ancestorId != null)
                                {
                                    e.Fields.Add("siteNode", ancestorId.ToString());
                                }
                            }
                            catch (Exception)
                            {
                                // Do nothing
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // Do nothing
                    }
                }
                else
                {
                    e.Fields.Add("siteNode", id);
                } 
            }
        }
    

    However, this no longer works and I cannot find any proper documentation on Our regarding how this should now be done.

    Does anyone know what I need to do so that this works with the latest version of Umbraco?

    Any help would be greatly appreciated.

  • Jason Espin 368 posts 1335 karma points
    Aug 26, 2015 @ 08:54
    Jason Espin
    0

    I have now re-engineered this to the following based upon this article however I now find that nothing is being saved to my custom index at all.

    using System;
    using System.Text;
    using System.Linq;
    using System.Xml.Linq;
    using System.Collections.Generic;
    using Examine;
    using umbraco.BusinessLogic;
    using System.Web;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    using Umbraco.Web.WebApi;
    using Umbraco.Web;
    using UmbracoExamine;
    
    namespace Lost_World_Adventures.App_Code.Examine_Extensions
    {
        public class AppEvents : IApplicationEventHandler
        {
            public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
            }
    
            public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
            }
    
            public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                var helper = new UmbracoHelper(UmbracoContext.Current);
                ExamineManager.Instance.IndexProviderCollection["AxumIndexer"].GatheringNodeData
                                    += (sender, e) => ExamineEvents.GatheringContentData(sender, e, helper);
            }
    
            public static void GatheringContentData(object sender, IndexingNodeDataEventArgs e, UmbracoHelper helper)
            {
    
            }
        }
    
        public static class ExamineEvents
        {
            public static void GatheringContentData(object sender, IndexingNodeDataEventArgs e, UmbracoHelper helper)
            {
                var content = helper.TypedContent(e.NodeId);
                var homepage = content.Ancestor("homepage");
    
                e.Fields.Add("siteNode", homepage.Id.ToString());
            }
        }
    }
    

    http://staheri.com/my-blog/2015/march/custom-examine-indexing-using-umbraco-cache/

  • Jason Espin 368 posts 1335 karma points
    Aug 26, 2015 @ 10:30
    Jason Espin
    100

    My final solution was this:

    public class AppEvents : IApplicationEventHandler
        {
            public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
            }
    
            public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
            }
    
            public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
    
                var helper = new UmbracoHelper(UmbracoContext.Current);
                ExamineManager.Instance.IndexProviderCollection["AxumIndexer"].GatheringNodeData
                                    += (sender, e) => ExamineEvents.GatheringContentData(sender, e, helper);
            }
        }
    
        public static class ExamineEvents
        {
            public static void GatheringContentData(object sender, IndexingNodeDataEventArgs e, UmbracoHelper helper)
            {
                try
                {
                    var content = helper.TypedContent(e.NodeId);
                    var homepage = content.AncestorOrSelf("homepage");
                    e.Fields.Add("siteNode", homepage.Id.ToString());
                }
                catch (System.NullReferenceException ex)
                {
                    if (e.Fields["level"] != "1")
                    {
                        IContentService cs = ApplicationContext.Current.Services.ContentService;
                        IContent[] unpublishedPageAncestors = cs.GetAncestors(e.NodeId).ToArray();
                        if (unpublishedPageAncestors.Where(x => x.ContentType.Alias.ToLower() == "homepage").Count() == 1)
                        {
                            int id = unpublishedPageAncestors.First().Id;
                            e.Fields.Add("siteNode", id.ToString());
                        }
                    }
                    else
                    {
                        e.Fields.Add("siteNode", e.Fields["id"]);
                    }
                }
                catch (Exception ex)
                {
                    // Do Nothing
                }
            }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft