Copied to clipboard

Flag this post as spam?

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


  • Kristoffer Eriksen 185 posts 465 karma points
    Oct 31, 2022 @ 12:34
    Kristoffer Eriksen
    0

    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

    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.

    Am I missing something?

Please Sign in or register to post replies

Write your reply to:

Draft