2

We have upgraded to Sitecore 10.1 from Sitecore 8.7.

In Sitecore 8.7 We used MultisiteItemLinkProvider which derives from LinkProvider and its obsolete in Sitecore 10.1.

In Sitecore 8.7 we use the following code in the answer. Here is the link

public class MultisiteItemLinkProvider : LinkProvider 

Which we upgraded to Sitecore 10.1 like this

public class MultisiteItemLinkProvider : ItemUrlBuilder
    {
        public MultisiteItemLinkProvider(DefaultItemUrlBuilderOptions defaultOptions) : base(defaultOptions)
        {
        }

        public override string Build(Item item, ItemUrlBuilderOptions options)
        {
            if (Sitecore.Context.Site == null || Sitecore.Context.Site.Name == "shell")
            {
                return base.Build(item, options);
            }
            return GetItemUrlFromBase(item, options);
        }

        protected virtual bool IsFromCurrentSite(Item item)
        {
            Assert.ArgumentNotNull(item, "item");
            return item.Paths.FullPath.Contains(Sitecore.Context.Site.RootPath);
        }

        protected virtual string GetItemUrlFromBase(Item item, ItemUrlBuilderOptions options)
        {
            if (IsFromCurrentSite(item)) return base.Build(item, options);

            var website = this.GetSiteContext(item);
            using (new SiteContextSwitcher(website))
            {
                options.AlwaysIncludeServerUrl = true;
                options.SiteResolving = true;
                return base.Build(item, options);
            }
        }

        private static List<KeyValuePair<string, Sitecore.Sites.SiteContext>> GetSites()
        {
            return SiteManager.GetSites()
                .Where(
                    s =>
                        !string.IsNullOrEmpty(s.Properties["rootPath"]) &&
                        !string.IsNullOrEmpty(s.Properties["startItem"]))
                .Select(
                    d => new KeyValuePair<string, Sitecore.Sites.SiteContext>($"{d.Properties["rootPath"]}{d.Properties["startItem"]}",
                        new Sitecore.Sites.SiteContext(new SiteInfo(d.Properties))))
                .ToList();
        }

        public virtual Sitecore.Sites.SiteContext GetSiteContext(Item item)
        {

            var site = GetSites().LastOrDefault(s => item.Paths.FullPath.ToLower().StartsWith(s.Key.ToLower()));
            return site.Value;
        }
    }

in the config

<links>
    <itemUrlBuilder type="Sitecore.Links.UrlBuilders.ItemUrlBuilder, Sitecore.Kernel">
        <patch:delete />
    </itemUrlBuilder>

    <itemUrlBuilder type="Foundation.Multisite.Providers.MultisiteItemLinkProvider, Foundation.Multisite">
        <param desc="defaultOptions" type="Sitecore.Links.UrlBuilders.DefaultItemUrlBuilderOptions, Sitecore.Kernel">
            <alwaysIncludeServerUrl>true</alwaysIncludeServerUrl>
            <languageEmbedding>always</languageEmbedding>
            <languageLocation>filePath</languageLocation>
            <lowercaseUrls>true</lowercaseUrls>
            <encodeNames>true</encodeNames>
            <useDisplayName>false</useDisplayName>
            <addAspxExtension>false</addAspxExtension>
            <siteResolving>true</siteResolving>
            <shortenUrls>true</shortenUrls>
        </param>

    </itemUrlBuilder>
</links>

In Sitecore 8.7 we are calling the method like this

LinkProvider multisiteItemLinkProvider = LinkManager.Providers["MultisiteItemLinkProvider"];

How can we call let's say MultisiteItemLinkProvider's GetItemUrl method in Sitecore 10.1?

2
  • Why did you inherit from ItemUrlBuilder? Should it not still be LinkProvider? The part that is obsolete is the constructor, not the class. Commented Apr 19, 2023 at 1:56
  • @MichaelWest I tried to use the existing LinkProvider code and it was working fine. The only issue I had was some of the auth sites were appending the prod site when clicking the search URL like "test1-sc10-auth.comhttps//test1-sc10.com/…". When I replaced the LinkProvider method with the ItemUrlBuilder then that issue was gone. Commented Apr 19, 2023 at 2:02

3 Answers 3

0

As you have prepared a custom item URL builder. You can inject a custom URL builder by patching the <links/itemUrlBuilder> configuration node.

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>

        <links>
            <itemUrlBuilder>
                <patch:attribute name="type">YourProjectNamespace.MultisiteItemLinkProvider, YourProjectNamespace</patch:attribute>
            </itemUrlBuilder>
        </links>
        <pipelines>
            <itemUrlBuilderExtensions>
            </itemUrlBuilderExtensions>
        </pipelines>
    </sitecore>
</configuration>

Also, worth to look these solutions.

  1. Create a foundation UrlBuilder with custom pipeline
  2. Extend from LocalizableLinkProvider If you are using SXA. The default LocalizableLinkProvider is actually allow us to extend but have to override GetItemUrl(Item, ItemUrlBuilderOptions) instead.

Credit: Multi custom links with Sitecore 10 using ItemUrlBuilder

Hope it helps!

1
  • Thanks Sumit but I have already included this in the config. I want to know how can I call let's say GetItemUrl method in the code. I have also updated my question that this config already exists. Commented Apr 19, 2023 at 3:20
0

Not sure if it will work for you but you can try your custom Link provider using this way.

var customProvider = ServiceLocator.GetRequiredResetableService<ProviderHelper<Sitecore.Links.LinkProvider, LinkProviderCollection>>()?.Value?.Provider as YourNamespace.LinkProvider;

Try to use this and see if you are able to achieve what you need.

Ref: How to call custom LinkProvider

0

I found this tutorial elsewhere, it worked for me: https://edwenkerk.hashnode.dev/multi-custom-links-with-sitecore-10-using-itemurlbuilder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.