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 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?
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.
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
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()
{
}
}
}
Umbraco 8 Events
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,
Have a look at this article https://creativewebspecialist.co.uk/2018/06/15/umbraco-v8-bye-bye-applicationeventhandler-hello-umbraco-components/
added this reference: https://www.perplex.nl/en/blog/2019/umbraco-v8-changes-for-developers/
Almost all the objects used in that post have been renamed :(
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?
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.
Hi Carlos,
The namespace to create components is this:
small example
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
Hi Marcio,
Thanks for your help. However, I'm still not able to implement that.
public class PublishEventsComposer : ComponentComposer
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
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
Hehe, we were posting at the same time! Happy it works for you now!
I'm not sure why it isn't working for you, here's a full example that works:
is working on a reply...