DEV Community

Morten Hartvig
Morten Hartvig

Posted on

Disable preview in Umbraco

When creating Umbraco projects you often end up making document types for content such as office locations, categories or employees where the content type does not have a template and/or is unable to be previewed directly.

Some clients might request to be redirected to the home page (or some related page) while others might request that the preview button is removed to avoid confusion. Below is an example of doing the latter.

Image description

Create a SendingContentNotificationHandler.cs to listen for content notifications and disable the preview for nodes without templates:

using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Notifications;

namespace Adventures
{
    public class SendingContentNotificationHandler : INotificationHandler<SendingContentNotification>
    {
        public SendingContentNotificationHandler() { }

        public void Handle(SendingContentNotification notification)
        {
            if (notification.Content.AllowedTemplates?.Count == 0)
            {
                notification.Content.AllowPreview = false;
            }
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Register your handler in Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddUmbraco(_env, _config)
        .AddBackOffice()
        .AddWebsite()
        .AddComposers()
        .AddNotificationHandler<SendingContentNotification, SendingContentNotificationHandler>()
        .Build();
}
Enter fullscreen mode Exit fullscreen mode

Image description

Top comments (0)