using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Notifications;
namespace MySite
{
public class DontShout : INotificationHandler<ContentPublishingNotification>
{
public void Handle(ContentPublishingNotification notification)
{
foreach (var node in notification.PublishedEntities)
{
if (node.ContentType.Alias.Equals("announcement"))
{
var newsArticleTitle = node.GetValue<string>("title");
if (newsArticleTitle.Equals(newsArticleTitle.ToUpper()))
{
notification.CancelOperation(new EventMessage("Corporate style guideline infringement",
"Don't put the announcement title in upper case, no need to shout!",
EventMessageType.Error));
}
}
}
}
}
}
I placed it in a test.cs file in the root of the project, but when I build the project and spins up the site again it seems not to work: I write "HEY" in the title field, but no notification turns up (except the "Content published: and is visible on the website" message).
#pragma warning disable IDE0022 // Use expression body for methods
services.AddUmbraco(_env, _config)
.AddBackOffice()
.AddWebsite()
.AddComposers()
.AddNotificationHandler<ContentPublishingNotification, DontShout>()
.Build();
#pragma warning restore IDE0022 // Use expression body for methods
I get this error: "The type or namespace name 'ContentPublishingNotification' could not be found"
Should I add a using statement in Startup.cs - and what statement?
How to "activate" custom code in v9?
Hi forum,
I am playing around with ContentService class and I am trying to get this example working (it's from https://our.umbraco.com/documentation/Reference/Notifications/ContentService-Notifications):
I placed it in a test.cs file in the root of the project, but when I build the project and spins up the site again it seems not to work: I write "HEY" in the title field, but no notification turns up (except the "Content published: and is visible on the website" message).
Hi Martin
You need to register your notification handlers in your startup too: https://our.umbraco.com/documentation/Reference/Notifications/#registering-notification-handlers-in-the-startup-class
Thanks - that makes sense.
But when I do that:
I get this error: "The type or namespace name 'ContentPublishingNotification' could not be found"
Should I add a using statement in Startup.cs - and what statement?
If you're in visual studio, put the cursor on the line where you add the handler, and press alt+enter, then it will suggest what you need 🙂
Fantastic - thanks! The "Show potential fixes" link helped me! :)
I was using VS Code (like the light weight feeling), but now I have a reason to use VS.
is working on a reply...