Copied to clipboard

Flag this post as spam?

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


  • john blair 48 posts 219 karma points
    Aug 25, 2019 @ 15:42
    john blair
    0

    V8 ContentService.Published event how to access the url of the published node

    I subscribe to the ContentService.Published event in umbraco 8. The PublishedEntities is a collection of IContent which has no Url property. How do I get the Url?

    Note: You would think that as this is the Published event - not the Publishing one...that you would be given the IPublishedContent which has a url property - but unfortunately not!

    Also how do you get an UmbracoHelper in the ContentService.Published event ... I'm thinking I could use that to access Content(id) method to get a IPublishedContent but again it seems Umbraco make it difficult to access this.

    Thanks for any help.

  • john blair 48 posts 219 karma points
    Aug 25, 2019 @ 15:48
    john blair
    0

    Seems a bit of a hack but I'm using :

    var helper = Umbraco.Web.Composing.Current.UmbracoHelper;
    var publishedContent = helper.Content(content.Id);
    var url = publishedContent.Url(mode: UrlMode.Absolute)
    
  • Tobias Klika 101 posts 570 karma points c-trib
    Aug 25, 2019 @ 16:53
    Tobias Klika
    102

    You can inject IUmbracoContextFactory in your component which allows you to retrieve published entities.

      public class PublishedComponent : IComponent
      {
        private IUmbracoContextFactory _umbracoContext;
    
    
        public PublishedComponent(IUmbracoContextFactory umbracoContext)
        {
          _umbracoContext = umbracoContext;
        }
    
        public void Initialize()
        {
          ContentService.Published += ContentService_Published;
        }
    
        public void Terminate()
        {
          ContentService.Published -= ContentService_Published;
        }
    
        private void ContentService_Published(IContentService sender, ContentPublishedEventArgs e)
        {
          using (UmbracoContextReference contextReference = _umbracoContext.EnsureUmbracoContext())
          {
            UmbracoContext context = contextReference.UmbracoContext;
            IPublishedContent content = context.Content.GetById(e.PublishedEntities.First().Id);
          }
        }
      }
    
  • john blair 48 posts 219 karma points
    Aug 26, 2019 @ 16:48
    john blair
    0

    Thanks a lot...that worked a treat.

    Per your suggestion it is better to inject what you need than just pull in a global UmbracoHelper.

    Now that I'm injecting what I need - I needed a logger in my component so injected that too to help with debugging.

    The server side is becoming more like the angularjs client side where you just inject the services you need.

    Would be nice if there was a list of available "injectable" services somewhere.

     public ErrorPagePublishedComponent(IUmbracoContextFactory umbracoContextFactory, ILogger log)
     {
            _umbracoContextFactory = umbracoContextFactory;
            _log = log;
      }
    
Please Sign in or register to post replies

Write your reply to:

Draft