Copied to clipboard

Flag this post as spam?

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


  • Carlos Gomes 38 posts 184 karma points
    Feb 28, 2019 @ 19:32
    Carlos Gomes
    0

    Hi guys,

    Since there is still no documentation for this, there is anyone that already know how to create an event in Umbraco v8 when the Application is Initialized?

    I want to create a node, based on property value, when an editor publishes another node. I used to do that in Umbraco 7 with the override method ApplicationInitialized.

    Thanks,

  • Travis Schoening 47 posts 173 karma points
    Feb 28, 2019 @ 19:35
  • Marcio Goularte 374 posts 1346 karma points
    Feb 28, 2019 @ 19:51
  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Feb 28, 2019 @ 20:22
    Søren Gregersen
    0

    Almost all the objects used in that post have been renamed :(

  • Carlos Gomes 38 posts 184 karma points
    Feb 28, 2019 @ 19:40
    Carlos Gomes
    0

    Hi Travis,

    Thanks for your quick response. I'm not able to refer the Umbraco.Core.Components as using statements. It says that there are no type or namespace Components under Umbraco.Core.

    I've tried to restore my nugget packages but I'm still not able to use the Umbraco.Core.Components as a statement. I can access the Umbraco.Core, but not the Components. Am I missing something?

  • Carlos Gomes 38 posts 184 karma points
    Feb 28, 2019 @ 20:08
    Carlos Gomes
    0

    I've tried to create a whole new project and I'm still not able to access that statement.

    My project is working correctly, the only thing that I can't do is create that component, since I'm not able to use the Umbraco.Core.Components statement.

  • Marcio Goularte 374 posts 1346 karma points
    Feb 28, 2019 @ 20:31
    Marcio Goularte
    1

    Hi Carlos,

    The namespace to create components is this:

    using Umbraco.Core.Composing;
    

    small example

        using Umbraco.Core;
        using Umbraco.Core.Composing;
    
        namespace UmbracoV8.Core
        {
    
            public class SomeComposer : IUserComposer
            {
                public void Compose(Composition composition)
                {
    
                    composition.Components().Append<MyComponent>();
                }
            }
    
            public class MyComponent : IComponent
            {
    
                public void Initialize()
                {
                    //stuff
    
                }
    
                public void Terminate()
                {
                    //throw new System.NotImplementedException();
                }
            }
        }
    
    public class PublishEventsComposer : ComponentComposer
    {
    }
    
    public class PublishEventsComponent : IComponent
    {
        private readonly IUserService _userService;
        private readonly ILogger _logger;
    
        public PublishEventsComponent(IUserService userService, ILogger logger)
        {
            _userService = userService;
            _logger = logger;
        }
    
        public void Initialize()
        {
            ContentService.Published += (contentService, e) =>
            {
                foreach (IContent ic in e.PublishedEntities)
                    if (_userService.GetUserById(ic.WriterId) is IUser author)
                        _logger.Info<PublishEventsComponent>(
                            $"Content '{ic.Name}' was published by '{author.Name}'");
            };
        }
    
        public void Terminate()
        {
        }
    }
    

    I believe that some namespace used in old examples have been changed

    https://www.perplex.nl/en/blog/2019/umbraco-v8-changes-for-developers/

    this article can help

  • Carlos Gomes 38 posts 184 karma points
    Mar 01, 2019 @ 10:49
    Carlos Gomes
    0

    Hi Marcio,

    Thanks for your help. However, I'm still not able to implement that.

    public class PublishEventsComposer : ComponentComposer

    public class PublishEventsComponent : IComponent
    {
        private readonly IUserService _userService;
        private readonly ILogger _logger;
    
        public PublishEventsComponent(IUserService userService, ILogger logger)
        {
            _userService = userService;
            _logger = logger;
        }
    
        public void Initialize()
        {
            ContentService.Published += (contentService, e) =>
            {
                foreach (IContent ic in e.PublishedEntities)
                    if (_userService.GetUserById(ic.WriterId) is IUser author)
                        _logger.Info<PublishEventsComponent>("this was saved");
            };
        }
    
        public void Terminate()
        {
        }
    }
    

    I don't have errors on my code but the event is not being fired when I publish a node, nor it passes by this method.

    I'm also appending my Component on the Compose method

  • Carlos Gomes 38 posts 184 karma points
    Mar 01, 2019 @ 11:01
    Carlos Gomes
    0

    It's working now.

    I was using a different interface to inherit for my Composer. It should be: PublishEventsComposer : IUserComposer.

    Thanks for all your help on this

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Mar 01, 2019 @ 11:04
    Sebastiaan Janssen
    0

    Hehe, we were posting at the same time! Happy it works for you now!

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Mar 01, 2019 @ 11:03
    Sebastiaan Janssen
    100

    I'm not sure why it isn't working for you, here's a full example that works:

    using Umbraco.Core;
    using Umbraco.Core.Composing;
    using Umbraco.Core.Events;
    using Umbraco.Core.Logging;
    using Umbraco.Core.Models;
    using Umbraco.Core.Models.Membership;
    using Umbraco.Core.Services;
    using Umbraco.Core.Services.Implement;
    
    namespace Cultiv.Hangfire
    {
        public class MyComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.Components().Append<MyComponent>();
            }
        }
    
        public class MyComponent : IComponent
        {
            private readonly IUserService _userService;
            private readonly ILogger _logger;
    
            public MyComponent(IUserService userService, ILogger logger)
            {
                _userService = userService;
                _logger = logger;
            }
            // initialize: runs once when Umbraco starts
            public void Initialize()
            {
                ContentService.Published += (contentService, e) =>
                {
                    foreach (IContent ic in e.PublishedEntities)
                        if (_userService.GetUserById(ic.WriterId) is IUser author)
                            _logger.Info<MyComponent>("this was published");
                };
            }
    
            // terminate: runs once when Umbraco stops
            public void Terminate()
            {
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft