Skip to content

Instantly share code, notes, and snippets.

@brendan-rice
Forked from warrenbuckley/DontShout.cs
Created July 2, 2021 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brendan-rice/b9b0dcd896624c9f829bc8d490355cec to your computer and use it in GitHub Desktop.
Save brendan-rice/b9b0dcd896624c9f829bc8d490355cec to your computer and use it in GitHub Desktop.
Umbraco V9 Event to Notification Handler Example
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));
}
}
}
}
}
}
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment