TransWikia.com

Custom Item Url Builder

Sitecore Asked by Ethan Schofer on August 23, 2021

Im trying to implement a custom Url Builder with Sitecore 9.3.I can’t seem to figure out how to patch it. I made a custom url builder that implements BaseUrlBuilder but when I try to patch it in I get:

Unable to cast object of type 'AmeriGasTransform.Foundation.ItemResolution.CustomItemUrlBuilder' to type 'Sitecore.Links.UrlBuilders.ItemUrlBuilder'.

Here is the current state of my patch file:

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
    <links>
        <itemUrlBuilder type="MySolution.Foundation.ItemResolution.CustomItemUrlBuilder,MySolution" 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>
                <siteResolving>true</siteResolving>
                <shortenUrls>true</shortenUrls>
            </param>
        </itemUrlBuilder>
    </links>
</sitecore>

One Answer

The update in Sitecore 9.3 adds the new itemUrlBuilder configuration to control how item and media URLs are built. Unfortunately, this in not wired into the IoC container (unlike the LinkManager itself for example). The Sitecore documentation states:

The default Sitecore implementation of the BaseLinkManager class uses the Sitecore.Links.LinkProvider class. The LinkProvider class uses the ItemUrlBuilder class to generate item URLs.

If you take a look at the default implementation of the LinkProvider class you will see that the Initialize method uses the Sitecore Config Factory to create the itemUrlBuilder object, which is of type ItemUrlBuilder (and not of type BaseUrlBuilder)

Since the sitecore/links/itemUrlBuilder is indirectly called via the LinkProvider, you should instead inherit from Sitecore.Links.UrlBuilders.ItemUrlBuilder and override the BuildUrl(item, options) method.

public class CustomItemUrlBuilder : Sitecore.Links.UrlBuilders.ItemUrlBuilder
{
    public override string BuildUrl(Item item, ItemUrlBuilderOptions options)
    {
        if (true) {
            // my custom url builder code
        } else {
            return base.BuildUrl(item, options);
        }
    }
}

Correct answer by jammykam on August 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP