Let’s Talk

We would love to hear from you. Want to know more about
our services or have any questions? Say Hi!

Sitecore 9.x Custom Link Provider

August 01, 2022
Sitecore 9.x Custom Link Provider
Keyur Garala
Keyur Garala
Sitecore Certified Architect
sitecore-9x-custom-link-provider

In the Sitecore 9.x website development, we have multilingual websites and we configured four websites like en-us, en-GB,en-au and en and we want item URL made like us,gb,au and en only but when we get the item URL from link provider, we will get URL with full language code.

So, we try to overcome this issue we override the link provider and updated GetItemUrl method. Please find below the code snippet which we have overridden to get the item URL with two letter language code.

When you check Sitecore config using URL (https://demosc.dev.local/sitecore/admin/showconfig.aspx) you will get information for linkprovider that use Sitecore default provider. Please refer below image for reference:

sitecore-9x-custom-link-provider-1

For our above issue we updated the below settings and created custom link provider.


sitecore-9x-custom-link-provider-2

We have created PipelineLinkProvider class which we inherited from LinkProvider and overrode GetItemUrl Method. Please refer below code snippet for your reference

                                
public class PipelineLinkProvider : Sitecore.Links.LinkProvider 
{ 
    public override String GetItemUrl(Item item, UrlOptions options) 
    { 
        if (IsLanguageEmbeddedUrl) 

            options.LanguageEmbedding = LanguageEmbedding.Always; 

        ResolveItemUrlPipelineArgs args = new ResolveItemUrlPipelineArgs(item, options); 

        CorePipeline.Run("resolveItemUrl", args); 

        if (args.IsResolved && !String.IsNullOrEmpty(args.Url)) 

            return ChangeLanguageUrlIfNeeded(args.Url); 

        return ChangeLanguageUrlIfNeeded(base.GetItemUrl(item, options)); 
    } 

    //for multi site, allow per site on/off of sitecore language in url. Default is false 

    public static bool IsLanguageEmbeddedUrl 

    { 
        get 
        { 
            if (Sitecore.Context.Site == null) 

                return false; 

            if (!string.IsNullOrEmpty(Sitecore.Context.Site.SiteInfo.Properties["languageEmbeddingUrl"])) 

                return bool.Parse(Sitecore.Context.Site.SiteInfo.Properties["languageEmbeddingUrl"]); 

            return false; //default 
        } 
    } 

    public string ChangeLanguageUrlIfNeeded(string url) 

    { 
        if (IsLanguageEmbeddedUrl && Sitecore.Context.Site.Name.Equals("corporate") && !string.IsNullOrEmpty(url)) 

        { 
            var trimmedUrl = url.TrimStart('/').ToLower(); 

            foreach (var entry in CorpCustomToScLanguageMapping) 
            { 

                if (trimmedUrl.StartsWith(entry.Value + "/", StringComparison.InvariantCultureIgnoreCase) || 

                    trimmedUrl.Equals(entry.Value, StringComparison.InvariantCultureIgnoreCase)) 
                { 
                    url = url.ReplaceFirst(entry.Value, entry.Key, StringComparison.InvariantCultureIgnoreCase); 
                    break; 
                } 
            } 
        } 
        return url; 
    } 

    private static Dictionary _corpCustomToScLanguageMapping; 
    public static Dictionary CorpCustomToScLanguageMapping 

    { 
        get 
        { 
            if (_corpCustomToScLanguageMapping == null) 
            { 
                _corpCustomToScLanguageMapping = new Dictionary() 
                { 
                    { "au", "en-au"}, 

                    { "uk", "en-gb"}, 

                    { "us", "en-us"}, 

                    { "en", "en"} 
                }; 
            } 
            return _corpCustomToScLanguageMapping; 
        } 
    } 
    private static Dictionary _corpScToCustomLanguageMapping; 
    public static Dictionary CorpScToCustomLanguageMapping 
    { 
        get 
        { 
            if (_corpScToCustomLanguageMapping == null) 

            { 
                _corpScToCustomLanguageMapping = CorpCustomToScLanguageMapping.ToDictionary(x => x.Value, x => x.Key); 
            } 
            return _corpScToCustomLanguageMapping; 
        } 
    } 
} 
                                
					        

The above example is overriding the link provider and also, we can implement multi-site logic to get URL in based on our requirement.


YOU MAY ALSO LIKE