Subscribing To Notifications
Subscribing to notifications allows you to execute custom code on a number of operations both before and after the operation occurs
Subscribing to notifications allows you to execute custom code on a number of operations both before and after the operation occurs. All you need to follow this guide is an Umbraco installation with some content, for example the Umbraco starter kit.
Subscribing to a notification
Let's add a string of text to the log when a document is published. (The log is useful for debugging, different parts of the Umbraco codebase 'log' key events, warnings and errors to the log)
We react to notifications in Umbraco inside a notification handler, so let's create one. Add a new C# class to our project - call it LogWhenPublishedHandler and use : INotificationHandler<ContentPublishedNotification>
to identify our code as a handler, that will handle ContentPublishedNotification
. We'll need to add using Umbraco.Cms.Core.Events;
to the top of the .cs file. Because the notifications that you can subscribe to are found in the core notifications namespace, we also need to add a using statement for that: using Umbraco.Cms.Core.Notifications;
.
We now have a class that looks like this:
However, we have an error and a red squiggly line under our class. This is because the INotificationHandler
interface expects us to implement a Handle
method for our notification. Let's create a public method called Handle
that takes a ContentPublishedNotification
as a parameter:
To check that this works, let's add a message to the log every time a content node is published.
We'll need to inject a Microsoft ILogger into our notification handler. This can be done by adding using Microsoft.Extensions.Logging;
to the top our file to add the required namespace. Next step is to add a constructor for our handler that allows Umbraco to inject the logger:
Now we can use the logger to send a message to the logs:
We could log the name of each item that is being published too:
Now we have a NotificationHandler
that logs the name of a piece of content every time it's published, however, we're not done yet.
Umbraco needs to know that our handler exists and that it handles ContentPublishedNotification
. To tell Umbraco this, we open up the Startup.cs
file in the root of the project. First, we need to add using Umbraco.Cms.Core.Notifications;
to the top of this file as well, once we've done this we need to find the ConfigureServices
method. We now add .AddNotificationHandler<ContentPublishedNotification, LogWhenPublishedHandler>()
to the ConfigureServices
method right before the Build()
part. The method now looks like this:
The entire handler class should look like this:
Now we're ready to spin up our site and give it a try. Go to the Umbraco backoffice and publish a piece of content. Switch to the Settings section and find the Log Viewer in the Settings tree:
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F65787334-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F8h0xdoteqcFVV1pGZgk0%252Fuploads%252Fgit-blob-d2010ec4570d1a34820fd71fa825b5a53838066f%252Flog-viewer.png%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=a8a8245d&sv=1)
Search 'All Logs', and if all is wired up correctly you should discover your custom publish log message entries:
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F65787334-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F8h0xdoteqcFVV1pGZgk0%252Fuploads%252Fgit-blob-ce3872a7669736a31b85571e545c0196434603aa%252Flog-messages.png%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=cf2f47c8&sv=1)
Before and after
As you can see our custom code has been executed when we published a piece of content. It executed after the item was published because we used the ContentPublishedNotification
. If you want to run code before publishing, use ContentPublishingNotification
. The same goes for most other notifications so Saving
: Saved
, Copying
: Copied
and so forth.
More information
Last updated