Copied to clipboard

Flag this post as spam?

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


  • Nathan 67 posts 146 karma points
    Sep 18, 2014 @ 14:26
    Nathan
    0

    Populate umbracoUrlName properties with their uppercase page names

    The default URL naming convention is the page node name in lower case.

    How can I populate all umbracoUrlName properties of all pages (except home page ) to their uppercase page name in content tree with a dash between words?  I'm trying to change the names of the URI segments to uppercase from http://our.umbraco.org/forum/umbraco-7/using-umbraco-7/NewTopic to http://our.umbraco.org/Forum/Umbraco-7/Using-Umbraco-7/New-Topic for SEO purposes. Something like in the last post from this topic: http://our.umbraco.org/forum/developers/extending-umbraco/7769-URL-rewriting-for-SEO-purposes

  • John C Scott 473 posts 1183 karma points
    Sep 18, 2014 @ 15:06
    John C Scott
    0

    I think this would definitely take some custom code to be written. One simple approach might be to have a publishing event that populates this field on

    save. http://our.umbraco.org/documentation/Reference/Events-v6/ContentService-Events

  • Nathan 67 posts 146 karma points
    Sep 18, 2014 @ 15:20
    Nathan
    0

    Can you give an example?

  • John C Scott 473 posts 1183 karma points
    Sep 18, 2014 @ 15:29
    John C Scott
    0

    OK give me a minute or two and I'll have a go

  • Nathan 67 posts 146 karma points
    Sep 18, 2014 @ 15:56
    Nathan
    0

    ok, thanks

  • John C Scott 473 posts 1183 karma points
    Sep 18, 2014 @ 16:47
    John C Scott
    0

    I'm not sure I've really understood the question properly, at least when I've finished what I've done it doesn't make much sense to me, but then I'm not an SEO expert, this will now give you an alternative URL in upper case, but the URL is not case sensitive anyway. Anyway I've done it...

    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    using Umbraco.Core.Publishing;
    using Umbraco.Core.Services;
    
        public class SaveEvent : ApplicationEventHandler
        {
            public SaveEvent()
            {
                ContentService.Publishing += ReplaceProperty;
            }
    
            private void ReplaceProperty(IPublishingStrategy sender, PublishEventArgs<IContent> args)
            {
                foreach (var node in args.PublishedEntities)
                {
                    string pageName = node.Name;
                    if (node.HasProperty("umbracoUrlName"))
                    {
                        node.SetValue("umbracoUrlName",pageName.ToUpper().Replace(" ","-"));
                    }
                }
            }
        }
    
    I've also created a visual studio project which you can compile and save the DLL into the /BIN folder of your site. You can download this from here:

     

     

     

  • Nathan 67 posts 146 karma points
    Sep 18, 2014 @ 16:58
    Nathan
    0

    so i shoud add this class somewhere else or just adding it to the ptoject and build it will work?

     

  • John C Scott 473 posts 1183 karma points
    Sep 18, 2014 @ 17:02
    John C Scott
    0

    if you put the DLL (from the bin folder) in the BIN folder of your site and your document type has a property called umbracoUrlName (the casing of the alias is important) then when you publish the node of that document type then the property will be replaced with the page name in upper case with the spaces replaced with hypens

  • John C Scott 473 posts 1183 karma points
    Sep 18, 2014 @ 17:08
    John C Scott
    0

    or I think you could place this code as a class in the app_code folder which would do the same thing

  • Nathan 67 posts 146 karma points
    Sep 18, 2014 @ 17:10
    Nathan
    0

    do I need to include (by right clicking > include in priject) the dll in the project? the rest of dll from bin are not included.

  • Nathan 67 posts 146 karma points
    Sep 18, 2014 @ 17:22
    Nathan
    0

    you mean the "CopyProperty.dll" ?

     

  • John C Scott 473 posts 1183 karma points
    Sep 18, 2014 @ 23:02
    John C Scott
    0

    yes that is the correct dll

    know you don't need to include it in the project

    although you could include the source to build into your project

    you don't need to reference it 

    it's just a self contained lirttle program doing that thing for you

    it could easily be a part of another project though

  • Nathan 67 posts 146 karma points
    Sep 19, 2014 @ 09:05
    Nathan
    0

    It doesn't work for me. I have the propery "umbracoUrlName" set in Master document type, so all pages inherit this propery. This is the steps I made, please tell me what am I missing:

    1. Created the "SaveEvent" class in "App_Code" folder:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using Umbraco.Core;
      using Umbraco.Core.Events;
      using Umbraco.Core.Models;
      using Umbraco.Core.Publishing;
      using Umbraco.Core.Services;
      
      
      namespace MyProjectName.App_Code
      {
          public class SaveEvent : ApplicationEventHandler
          {
              public SaveEvent()
              {
                 ContentService.Publishing += ReplaceProperty;
              }
      
      
      
          private void ReplaceProperty(IPublishingStrategy sender, PublishEventArgs&lt;IContent&gt; args)
          {
              foreach (var node in args.PublishedEntities)
              {
                  string pageName = node.Name;
                  if (node.HasProperty("umbracoUrlName"))
                  {
                      node.SetValue("umbracoUrlName", pageName.ToUpper().Replace(" ", "-"));
                  }
              }
          }
      }
      
      }
    2. Copied the dll "CopyProperty.dll" from your project's bin folder into my project's bin folder, didn't inlcude in project, build the project.

    3. BO: Right click Home node > Publish Home and all subpages, also right click on Content > Publish entire site.

    After this, "Link to document" property is the same (lowercase), and "umbracoUrlName" prrperty is not populated.

    What am I missing?

    Thanks.

  • John C Scott 473 posts 1183 karma points
    Sep 19, 2014 @ 12:01
    John C Scott
    0

     I'm not sure how your project is set up.

    For the dll to work it would need to be in the /bin folder of your site.

    If you are building a project which is placing a dll into the /bin folder of your site then the best way to include this would be to include this class into any project which builds into the bin folder of your site.

    You can debug this by attaching to the w3wp.exe process within visual studio and setting a breakpoint within the class above, This will then tell you if the problem is that the event is not firing, or if it is firing then if there is a problem with naming etc.

     

Please Sign in or register to post replies

Write your reply to:

Draft