Copied to clipboard

Flag this post as spam?

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


  • Alexander Bush 4 posts 93 karma points
    Aug 22, 2019 @ 14:29
    Alexander Bush
    0

    Setting umbracoUrlName programmatically

    Hello, I am trying to set the umbracoUrlName for a collection of nodes using the Umbraco Api.

    I am pretty much just looping through a bunch of nodes, setting the "umbracoUrlName" field then finally saving and publishing them.

    var Nodes = Services.ContentService.GetContentOfContentType(37180);
    
            foreach(var node in Nodes)
            {
                var parties = node.GetValue("parties");
                var heading = node.GetValue("heading");
    
                string newVal = heading + " - " + parties;
    
                node.Name = newVal;
                node.SetValue("heading", newVal);
    
                string urlFriendly = newVal.ToUrlSegment();
    
                node.SetValue("umbracoUrlName", urlFriendly);                
    
                if(node.Published)
                {
                    Services.ContentService.SaveAndPublishWithStatus(node, 0, true);
                }
                else
                {
                    Services.ContentService.Save(node);
                }                
            }
    

    The fields get populated as expected but the new url does not automatically change unless I manually login to Umbraco itself and click "Save and Publish".

    Does anyone have any suggestions please? I really need the Url to automatically apply during this loop, there are too many nodes to manually go and save them all again.

  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Aug 22, 2019 @ 17:44
    Shaishav Karnani from digitallymedia.com
    0

    You need to just clear your cache and new url should appear in the cache.

    You can create a method and call this at the end of your code.

        private void ClearCache()
        {
            try
            {
                //Clear all output cache so everything is refreshed.
                var cacheManager = new OutputCacheManager();
                cacheManager.RemoveItems();
            }
            catch (Exception ex)
            {
                LogHelper.Error<UmbracoEvents>($"Exception: {ex.Message} - StackTrace: {ex.StackTrace}", ex);
            }
        }
    

    Please try and let me know how it goes for you.

  • Alexander Bush 4 posts 93 karma points
    Sep 27, 2019 @ 13:35
    Alexander Bush
    100

    Hello, sorry for the delay in reply to this and thanks for your suggestion.

    I tried your method but unfortunately it did not work for me i'm afraid, but I think it was down to how I was debugging and testing my code.

    I ended up running the code on our live server/database and everything got published as intended.

    My guess is whilst debugging this I was not allowing the system to run any background events that may get triggered after a node save.

Please Sign in or register to post replies

Write your reply to:

Draft