Additional preview environments support
Configure custom preview URLs to provide editors with seamless access to external preview environments for the Content Delivery API data.
Configuring custom preview URLs
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.PublishedCache;
namespace Umbraco.Docs.Samples;
public class AdditionalPreviewUrlsNotificationHandler : INotificationHandler<SendingContentNotification>
{
private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor;
public AdditionalPreviewUrlsNotificationHandler(IPublishedSnapshotAccessor publishedSnapshotAccessor)
=> _publishedSnapshotAccessor = publishedSnapshotAccessor;
public void Handle(SendingContentNotification notification)
{
foreach (ContentVariantDisplay variantDisplay in notification.Content.Variants.Where(variant => variant.PublishDate.HasValue))
{
// Retrieve the route of each content item
IPublishedSnapshot publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
var route = publishedSnapshot.Content?.GetRouteById(true, notification.Content.Id, variantDisplay.Language?.IsoCode);
if (route == null)
{
continue;
}
route = route.TrimStart('/');
variantDisplay.AdditionalPreviewUrls = new[]
{
new NamedUrl
{
// Dynamically generate Preview URL
Name = $"Development{(variantDisplay.Language != null ? $" ({variantDisplay.Language.Name})" : null)}",
Url = $"https://dev.environment.org/{route}?culture={variantDisplay.Language?.IsoCode}&preview=true"
},
new NamedUrl
{
// Dynamically generate Preview URL
Name = $"Staging{(variantDisplay.Language != null ? $" ({variantDisplay.Language.Name})" : null)}",
Url = $"https://staging.environment.org/{route}?culture={variantDisplay.Language?.IsoCode}&preview=true"
}
};
}
}
}Accessing preview environments



Last updated
Was this helpful?