Copied to clipboard

Flag this post as spam?

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


  • Martin Rud 261 posts 1022 karma points c-trib
    Mar 20, 2022 @ 16:32
    Martin Rud
    0

    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):

    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).

  • Søren Kottal 713 posts 4571 karma points MVP 6x c-trib
    Mar 20, 2022 @ 17:00
    Søren Kottal
    0

    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

  • Martin Rud 261 posts 1022 karma points c-trib
    Mar 20, 2022 @ 20:31
    Martin Rud
    0

    Thanks - that makes sense.

    But when I do that:

    #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?

  • Søren Kottal 713 posts 4571 karma points MVP 6x c-trib
    Mar 20, 2022 @ 21:47
    Søren Kottal
    100

    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 🙂

  • Martin Rud 261 posts 1022 karma points c-trib
    Mar 21, 2022 @ 04:56
    Martin Rud
    0

    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.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies