Custom links with Sitecore 9.3 using UrlBuilders

So if you are currently migrating from an older Sitecore solution into Sitecore 9.3, you will notice that the LinkProvider is no longer the way to approach custom urls. Instead you need to extend Sitecore.Links.UrlBuilders.ItemUrlBuilder. The main method to override is Build.

Here is an example of the custom code used:


        public override string Build(Sitecore.Data.Items.Item item, ItemUrlBuilderOptions options)
        {
            if (Util.UsesCustomUrl(item))
            {

                // handle custom logic

            }
            else if(Util.IsWebServiceData(item))
            {
                //Additional custom logic
            }else{
                ItemUrlBuilderOptions itemUrlBuilderOptions = new ItemUrlBuilderOptions();
                itemUrlBuilderOptions.LanguageEmbedding = LanguageEmbedding.Never;
                itemUrlBuilderOptions.LowercaseUrls = true;

                return base.Build(item, options);
            }

        }




Patching the configuration file

 <?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <links>
      <itemUrlBuilder type="Your-custom-path.CustomUrlBuilder,Your-custom-dll" patch:instead="*[@type='Sitecore.Links.UrlBuilders.ItemUrlBuilder, Sitecore.Kernel']">
        <param type="Sitecore.Links.UrlBuilders.DefaultItemUrlBuilderOptions, Sitecore.Kernel" desc="defaultOptions">
          <alwaysIncludeServerUrl ref="links/urlBuilder/alwaysIncludeServerUrl"/>
          <languageEmbedding ref="links/urlBuilder/languageEmbedding"/>
          <languageLocation ref="links/urlBuilder/languageLocation"/>
          <lowercaseUrls ref="links/urlBuilder/lowercaseUrls"/>
          <encodeNames ref="links/urlBuilder/encodeNames"/>
          <useDisplayName ref="links/urlBuilder/useDisplayName"/>
          <addAspxExtension>false</addAspxExtension>
        </param>
      </itemUrlBuilder>
    </links>
  </sitecore>
</configuration>

Comments

Post a Comment