Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sitecore Site Specific Link Provider for up to SC 8.1
using System;
using System.Collections.Specialized;
using System.Web;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Links;
using Sitecore.Web;
namespace Sitecore.Custom.Links
{
public class SwitchingLinkProvider : Sitecore.Links.LinkProvider
{
private const String FALLBACK_ATTRIBUTE_KEY = "fallback";
private const String LINKPROVIDER_ATTRIBUTE_KEY = "linkProvider";
private string Fallback { get; set; }
public override void Initialize(string name, NameValueCollection config)
{
base.Initialize(name, config);
this.Fallback = config[FALLBACK_ATTRIBUTE_KEY];
Assert.IsNotNullOrEmpty(this.Fallback, "fallback");
}
private Sitecore.Links.LinkProvider SiteLinkProvider
{
get
{
var siteLinkProvider = (Sitecore.Context.Site != null)
? Sitecore.Context.Site.Properties[LINKPROVIDER_ATTRIBUTE_KEY] : String.Empty;
if (String.IsNullOrEmpty(siteLinkProvider))
siteLinkProvider = this.Fallback;
return LinkManager.Providers[siteLinkProvider]
?? LinkManager.Providers[this.Fallback];
}
}
#region LinkProvider Options
public override bool AddAspxExtension
{
get
{
return SiteLinkProvider.AddAspxExtension;
}
}
public override bool AlwaysIncludeServerUrl
{
get
{
return SiteLinkProvider.AlwaysIncludeServerUrl;
}
}
public override bool EncodeNames
{
get
{
return SiteLinkProvider.EncodeNames;
}
}
public override LanguageEmbedding LanguageEmbedding
{
get
{
return SiteLinkProvider.LanguageEmbedding;
}
}
public override LanguageLocation LanguageLocation
{
get
{
return SiteLinkProvider.LanguageLocation;
}
}
public override bool LowercaseUrls
{
get
{
return SiteLinkProvider.LowercaseUrls;
}
}
public override bool ShortenUrls
{
get
{
return SiteLinkProvider.ShortenUrls;
}
}
public override bool UseDisplayName
{
get
{
return SiteLinkProvider.UseDisplayName;
}
}
#endregion
#region LinkProvider Methods
public override string ExpandDynamicLinks(string text, bool resolveSites)
{
return SiteLinkProvider.ExpandDynamicLinks(text, resolveSites);
}
public override UrlOptions GetDefaultUrlOptions()
{
return SiteLinkProvider.GetDefaultUrlOptions();
}
public override string GetItemUrl(Item item, UrlOptions options)
{
return SiteLinkProvider.GetItemUrl(item, options);
}
public override string GetDynamicUrl(Item item, LinkUrlOptions options)
{
return SiteLinkProvider.GetDynamicUrl(item, options);
}
public override bool IsDynamicLink(string linkText)
{
return SiteLinkProvider.IsDynamicLink(linkText);
}
public override DynamicLink ParseDynamicLink(string linkText)
{
return SiteLinkProvider.ParseDynamicLink(linkText);
}
public override RequestUrl ParseRequestUrl(HttpRequest request)
{
return SiteLinkProvider.ParseRequestUrl(request);
}
#endregion
}
}
<linkManager>
<providers>
<add name="provider-site1" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" addAspxExtension="false" languageEmbedding="never" useDisplayName="true" ... />
<add name="provider-site2" type="Sitecore.Custom.LinkProvider, Sitecore.Custom" alwaysIncludeServerUrl="true" languageEmbedding="always" lowercaseUrls="true" />
</providers>
</linkManager>
<sites>
<site name="site1" linkProvider="provider-site1" ... />
<site name="site2" linkProvider="provider-site2" ... />
<site name="site3" ... />
</sites>
<linkManager>
<patch:attribute name="defaultProvider" value="switcher" />
<providers>
<add name="switcher" fallback="sitecore" type="Sitecore.Custom.Links.SwitchingLinkProvider, Sitecore.Custom" />
</providers>
</linkManager>
@arvindchourasiya

This comment has been minimized.

Copy link

commented Jul 22, 2016

Hello Kamruz ,
Good explanation. But i'm trying to same thing in Multi-site configuration i'm confused. its not working properly.
then can you please suggests me about this.

@yiangos

This comment has been minimized.

Copy link

commented Jun 5, 2017

Hello.

Thank you for providing this gist.

LinkProviders["provider"] is considered obsolete in SC 8.2 update 1. Do you happen to know of an alternative? I can't seem to find any.

@mrosiak

This comment has been minimized.

Copy link

commented Sep 6, 2017

@yiangos
LinkManager.Providers is equivalent to ServiceLocator.GetRequiredResetableService<Sitecore.Configuration.ProviderHelper<LinkProvider, LinkProviderCollection>>().Value.Providers

That will only hide obsolete message, so better approach would be replacing it with BaseLinkManager. Check that git repo for implementation example
https://gist.github.com/sergeyshushlyapin/90a0edd9958a77e5cd8772d0c15bf71b

@yiangos

This comment has been minimized.

Copy link

commented Sep 8, 2017

@mrosiak

Thanks for the heads up and the gist. I'll be looking into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.