Copied to clipboard

Flag this post as spam?

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


  • Rob Carlaw 21 posts 134 karma points
    May 31, 2017 @ 15:59
    Rob Carlaw
    0

    ApplicationEventHandler - publish content event - convert IContent to model builder

    hi,

    I have an applicationeventhandler that listens for the publish of content if it's of type Product it needs to do some extra stuff.

    However whenever I try TryToConvert<> it fails to convert even though i can see it's of type Product. Think I'm missing something?

    Code is here:

        public class ProductPublishHook : ApplicationEventHandler
        {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication,  ApplicationContext applicationContext)
        {
            core.ContentService.Published += ContentServicePublished;
        }
        private void ContentServicePublished(IPublishingStrategy sender, PublishEventArgs<IContent> args)
        {
            foreach (IContent node in args.PublishedEntities)
            {
                if (node.ContentType.Alias == "product")
                {
                    Attempt<Product> productAttempt = node.TryConvertTo<Product>();
    
                    // more login here
                }
            }
        }
    }
    

    TryConvertTo says it's unable to cast. Product only implments IPublishedContent. daaaaa

    anyone got an idea?

Please Sign in or register to post replies

Write your reply to:

Draft