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
    May 07, 2015 @ 18:46
    Jason Espin
    0

    Accessing Umbraco.TypedContent from controller inheriting ApplicationBase

    Hi all,

    I am currently working through Ismail Mayat's excellent tutorial relating to massaging data before including it in your examine index.

    http://thecogworks.co.uk/blog/posts/2012/november/examiness-hints-and-tips-from-the-trenches-part-2/

    One major shortcoming of Umbraco I find is that information such as the culture of a page is not easily obtainable unless you are using an IpublishedContent object and the GetCulture method. So, my idea was to add to my examine index a culture indicator so I could use this for processing my examine results when I run a search.

    My question is, how would I access the Umbraco.TypedContent() method in my class that inherits ApplicationBase?

    I've included all of the relevant using statements that I would normally add but I don't seem to be able to access this method.

    My code can be found below:

    using Examine;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using umbraco.BusinessLogic;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    using Umbraco.Web.WebApi;
    using Umbraco.Web;
    
    namespace Project.App_Code.Examine_Extensions
    {
        public class ExamineEvents : ApplicationBase
        {
            public ExamineEvents()
            {
                ExamineManager.Instance.IndexProviderCollection["ToursIndexer"].GatheringNodeData += ExamineEvents_GatheringNodeData;
            }
    
            void ExamineEvents_GatheringNodeData(object sender, IndexingNodeDataEventArgs e)
            {
                AddToContentsField(e);
            }
    
            private void AddToContentsField(IndexingNodeDataEventArgs e)
            {
                var fields = e.Fields;
                string value = string.Empty;
                fields.TryGetValue("id", out value);
                if (value != string.Empty)
                {
                    // run umbraco.TypedContent(value)
                }
            }
        }
    }
    

    What I am trying to do is when a page is saved, I will use its node id from the index and then try and convert this to an Ipublished content object so that I can run the GetCulture() method on it then inject this as a new field in my index. If this is not possible, what is the alternative?

    Any help would be greatly appreciated.

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    May 07, 2015 @ 19:16
    Ismail Mayat
    0

    There is blog post here http://staheri.com/my-blog/2015/march/custom-examine-indexing-using-umbraco-cache/ see how todo it

    Regards

    Ismail

  • Jason Espin 368 posts 1335 karma points
    May 08, 2015 @ 10:42
    Jason Espin
    0

    Hi Ismail,

    Thanks for sending that through. I've managed to get everything working from that example. The annoying thing is now finding I can't do what I want to do because once again Umbraco functionality is broken.

    When I try and do this I get object not set to an instance of an object on the GetCulture() method. :-(

    using Examine;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using umbraco.BusinessLogic;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    using Umbraco.Web.WebApi;
    using Umbraco.Web;
    
    namespace project.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["ToursIndexer"].GatheringNodeData 
                    += (sender, e) => AppEvents.GatheringContentData(sender, e, helper);
            }
    
            public static void GatheringContentData(object sender, IndexingNodeDataEventArgs e, UmbracoHelper helper)
            {
                IPublishedContent content = helper.TypedContent(e.NodeId);
                string culture = content.GetCulture().ToString();
                e.Fields.Add("culture", culture);
            }
        }
    }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies