I have a running solution in Umbraco 8, where the destination-url in the database gets updated on publish.
The reason for this is that the client creates redirects before the page is published, and the destination-url in the database doesn't update when the page is published later on.
The running code in Umbraco 8 is
private void ContentService_Published(IContentService sender, ContentPublishedEventArgs e)
{
var node = e.PublishedEntities.FirstOrDefault();
if (node == null)
{
return;
}
var redirectItems = _redirectsService.GetRedirectsByContentId(node.Id);
if (!redirectItems.Any())
{
return;
}
using (_scopeProvider.CreateScope(autoComplete: true))
{
using (var contextReference = _contextService.EnsureUmbracoContext())
{
var context = contextReference.UmbracoContext;
var content = context.Content.GetById(node.Id);
if (!content.IsPublished())
{
return;
}
var url = content.Url;
foreach (var item in redirectItems)
{
item.LinkUrl = url;
_redirectsService.SaveRedirect(item);
}
}
}
}
In Umbaco 10, I'm hooking in to the ContentPublishedNotification with the basic same code:
public void Handle(ContentPublishedNotification notification)
{
var node = notification.PublishedEntities.FirstOrDefault();
if (node == null)
{
return;
}
IRedirect[] redirectItems = _redirectsService.GetRedirectsByNodeId(RedirectDestinationType.Content, node.Id);
if (!redirectItems.Any())
{
return;
}
using (_scopeProvider.CreateScope(autoComplete: true))
{
using (var contextReference = _contextService.EnsureUmbracoContext())
{
var context = contextReference.UmbracoContext;
var content = context?.Content?.GetById(node.Id);
if (content == null || !content.IsPublished())
{
return;
}
var url = content?.Url();
foreach (var redirect in redirectItems)
{
redirect.Destination.Url = url;
_redirectsService.SaveRedirect(redirect);
}
}
}
}
It runs in publish, and it finds the correct entry in the database. But .. It doesn't update the entry in the database on the saveredirect.method.
If I change for value for the redirect.Url (the "alias") it works fine, but the value for redirect.Destination.Url don't get overwritten.
Change redirect-destination url on publish
I have a running solution in Umbraco 8, where the destination-url in the database gets updated on publish.
The reason for this is that the client creates redirects before the page is published, and the destination-url in the database doesn't update when the page is published later on.
The running code in Umbraco 8 is
In Umbaco 10, I'm hooking in to the ContentPublishedNotification with the basic same code:
It runs in publish, and it finds the correct entry in the database. But .. It doesn't update the entry in the database on the saveredirect.method. If I change for value for the redirect.Url (the "alias") it works fine, but the value for redirect.Destination.Url don't get overwritten.
Am I missing something?
is working on a reply...