Created
May 5, 2021 10:49
-
-
Save warrenbuckley/af711f185a1b2a2e955683f68533f181 to your computer and use it in GitHub Desktop.
Umbraco V9 Event to Notification Handler Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Umbraco.Cms.Core.Events; | |
using Umbraco.Cms.Infrastructure.Services.Notifications; | |
namespace TestingEvents | |
{ | |
public class DontShout : INotificationHandler<ContentPublishingNotification> | |
{ | |
public void Handle(ContentPublishingNotification notification) | |
{ | |
foreach (var node in notification.PublishedEntities) | |
{ | |
if (node.ContentType.Alias == "corporateNewsAnnouncement") | |
{ | |
var newsArticleTitle = node.GetValue<string>("newsTitle"); | |
if (newsArticleTitle.Equals(newsArticleTitle.ToUpper())) | |
{ | |
// Stop putting news article titles in upper case, so cancel publish | |
// notification.Cancel = true; | |
// Explain why the publish event is cancelled | |
// notification.Messages.Add(new EventMessage("Corporate style guideline infringement", "Don't put the news article title in upper case, no need to shout!", EventMessageType.Error)); | |
// Above two lines - simplified | |
notification.CancelOperation(new EventMessage("Corporate style guideline infringement", "Don't put the news article title in upper case, no need to shout!", EventMessageType.Error)); | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using System; | |
using TestingEvents; | |
using Umbraco.Cms.Core.DependencyInjection; | |
using Umbraco.Cms.Infrastructure.Services.Notifications; | |
using Umbraco.Extensions; | |
namespace Umbraco.Cms.Web.UI.NetCore | |
{ | |
public class Startup | |
{ | |
private readonly IWebHostEnvironment _env; | |
private readonly IConfiguration _config; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="Startup"/> class. | |
/// </summary> | |
/// <param name="webHostEnvironment">The Web Host Environment</param> | |
/// <param name="config">The Configuration</param> | |
/// <remarks> | |
/// Only a few services are possible to be injected here https://github.com/dotnet/aspnetcore/issues/9337 | |
/// </remarks> | |
public Startup(IWebHostEnvironment webHostEnvironment, IConfiguration config) | |
{ | |
_env = webHostEnvironment ?? throw new ArgumentNullException(nameof(webHostEnvironment)); | |
_config = config ?? throw new ArgumentNullException(nameof(config)); | |
} | |
/// <summary> | |
/// Configures the services | |
/// </summary> | |
/// <remarks> | |
/// This method gets called by the runtime. Use this method to add services to the container. | |
/// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 | |
/// </remarks> | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
#pragma warning disable IDE0022 // Use expression body for methods | |
services.AddUmbraco(_env, _config) | |
.AddBackOffice() | |
.AddWebsite() | |
.AddComposers() | |
.AddNotificationHandler<ContentPublishingNotification, DontShout>() // Added this to register this with Umbraco | |
.Build(); | |
#pragma warning restore IDE0022 // Use expression body for methods | |
} | |
/// <summary> | |
/// Configures the application | |
/// </summary> | |
public void Configure(IApplicationBuilder app) | |
{ | |
app.UseUmbracoBackOffice(); | |
app.UseUmbracoWebsite(); | |
} | |
} | |
} |
Hi @MarGraz
I think I mentioned in the video the namespace will change to help discoverability, I suggest that you update to a nightly build that would have had the same changes I did in that video.
Thanks
Warren
@warrenbuckley thank you for your reply.
I added the nightly source "https://www.myget.org/F/umbraconightly/api/v3/index.json" in the Visual Studio "Package sources", and I found all the nightly versions. Do you recommend the nightly for a future production environment, or it's better if I continue with the "beta-00x"?
Thank you
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Warren, I'm using Umbraco 9 (beta-001) where I'm trying to implement my old event handler. Following your video, in my case, the namespace "Umbraco.Cms.Infrastructure.Services.Notifications" is not available.
Instead, I found the "ContentPublishingNotification" class in "Umbraco.Cms.Core.Services.Notifications".
In the meantime something has changed?
Thank you