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?