Copied to clipboard

Flag this post as spam?

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


  • Valerie 67 posts 163 karma points
    Aug 20, 2014 @ 14:16
    Valerie
    0

    Published Event - How to get Url? Elastic Search

    Hello,

    I'm trying to hook into the published event so I can index a node in Elastic Search.  However, I would like to store the url to be consumed on the front end.

    It works fine on existing content that I am updating, but not on new content.

    When I use the UmbracoHelper to get TypedContent it returns null because it isn't in the cache and umbraco.library.NiceUrl(id) doesn't work either.

    Please help - this is a real deal breaker for integrating a 3rd party search solution.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 20, 2014 @ 15:13
    Jeavon Leopold
    0

    Hi Valerie,

    Unpublished nodes don't have a URL, it's the publishing that creates it.

    Could you share your event code so we can see what's going on?

    Jeavon

  • Valerie 67 posts 163 karma points
    Aug 20, 2014 @ 16:33
    Valerie
    0

    Hi there,

    It's pretty basic (I've omitted the 2 unused methods from the interface). I'm using 7.1.4.

    The issue is that "content" below returns null when it's a newly created item and I can't use IContent either (I don't really want to since my indexer consumes IPublishedContent) since it hasn't got a url.

     public class ContentEvents : IApplicationEventHandler

        {

            private readonly SearchService searchService = new SearchService();

            void ContentServicePublished(Umbraco.Core.Publishing.IPublishingStrategy sender, PublishEventArgs<IContent> e)

            {

                foreach (var item in e.PublishedEntities)

                {

    //this is null for a newly created item

                    var content = new UmbracoHelper(UmbracoContext.Current).TypedContent(item.Id);

                    searchService.Index(content);

                }

            }

            public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)

            {

                ContentService.Published += ContentServicePublished;

                ContentService.Deleted += ContentServiceDeleted;

            }

            private void ContentServiceDeleted(IContentService sender, DeleteEventArgs<IContent> e)

            {

                foreach (var item in e.DeletedEntities)

                {

                    searchService.Delete(item.Id);

                }

            }

        }

     

    Thanks,

     

    Valerie

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 20, 2014 @ 17:56
    Jeavon Leopold
    0

    Hmm, strangely NiceUrl does return while TypedContent doesn't. Should work for you though...?

    public class ContentEvents : ApplicationEventHandler
    {
    
        //private readonly SearchService searchService = new SearchService();
    
        void ContentServicePublished(Umbraco.Core.Publishing.IPublishingStrategy sender, PublishEventArgs<IContent> e)
        {
    
            foreach (var item in e.PublishedEntities)
            {
                var contentUrl = new UmbracoHelper(UmbracoContext.Current).NiceUrl(item.Id);
    
                //searchService.Index(content);
    
            }
    
        }
    
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
    
            ContentService.Published += ContentServicePublished;
    
            ContentService.Deleted += ContentServiceDeleted;
    
        }
    
        private void ContentServiceDeleted(IContentService sender, DeleteEventArgs<IContent> e)
        {
    
            foreach (var item in e.DeletedEntities)
            {
    
               // searchService.Delete(item.Id);
    
            }
    
        }
    }  
    
  • Valerie 67 posts 163 karma points
    Aug 20, 2014 @ 18:18
    Valerie
    0

    It just returns a "#" when I do it :(

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 20, 2014 @ 22:33
    Jeavon Leopold
    100

    Hi Valerie,

    That is strange, in my v7.1.4 it seems to be there.

    Anyhow, I had a deeper look and the problem is that the publish events happens before the published content is updated. So I have found that there is a legacy event called AfterUpdateDocumentCache which is called after the published content has been updated, there doesn't appear to be a current equivalent. I think this should do what you need:

    using umbraco;
    using umbraco.cms.businesslogic.web;
    
    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    using Umbraco.Web;
    
    public class ContentEvents : ApplicationEventHandler
    {
    
        //private readonly SearchService searchService = new SearchService();
    
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Deleted += ContentServiceDeleted;
    
            content.AfterUpdateDocumentCache += content_AfterUpdateDocumentCache;
    
        }
    
        void content_AfterUpdateDocumentCache(Document sender, umbraco.cms.businesslogic.DocumentCacheEventArgs e)
        {
    
            // if you want to use Content Service instead of legacy Document Api
            var cs = ApplicationContext.Current.Services.ContentService;
            var item = cs.GetById(sender.Id);
    
            var contentUrl = new UmbracoHelper(UmbracoContext.Current).TypedContent(item.Id);
        }
    
        private void ContentServiceDeleted(IContentService sender, DeleteEventArgs<IContent> e)
        {
    
            foreach (var item in e.DeletedEntities)
            {
    
                // searchService.Delete(item.Id);
    
            }
    
        }
    }  
    

    Jeavon

  • Valerie 67 posts 163 karma points
    Aug 21, 2014 @ 10:18
    Valerie
    0

    Thanks a lot Jeavon that solves my problem completely! So happy ;)

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 21, 2014 @ 10:38
    Jeavon Leopold
    0

    That's great! There is ongoing work on a new object cache for Umbraco vNext which will hopefully also come with new cache events, in the meantime this should be fine.

    Please mark the above correct solution I think it will be useful to others :)

  • Søren Kottal 713 posts 4571 karma points MVP 6x c-trib
    Mar 01, 2016 @ 13:46
    Søren Kottal
    0

    Sorry for bringing up an old topic.

    I am having the same problem (in 7.4.1), ContentService.Published fires before the cache is updated, meaning Umbraco.TypedContent gets the previous version.

    Is there an issue created for this?

  • Harvey 28 posts 122 karma points
    Sep 13, 2016 @ 10:40
    Harvey
    0

    I can't see an issue yet for this. Have you posted one?

  • Harvey 28 posts 122 karma points
    Sep 13, 2016 @ 14:38
  • deepak dubey 4 posts 74 karma points
    May 28, 2018 @ 07:42
    deepak dubey
    0

    how i can get the current page & its content on the publish event , in my case i have multiple pages , so which page has been published i want to know & get its Id not the whole pages or blocks .

    Please Help me i am new to Umbraco

Please Sign in or register to post replies

Write your reply to:

Draft