We have a multi-site solution (Site 1 and Site 2), and I need to be able to determine if an item for which we're getting the URL (in the LinkProvider, which is custom) belongs to the current context site (Sitecore.Context.Site
), or is part of a different site. Is there a good way to do this?
Basically, we just need to be able to find out to which site the item is associated. We can do the comparison between that value and the current context site.
Approach 1 – Fetch Current Page URL in Sitecore We can use the LinkManager. GetItemURL and pass the current item as a parameter to fetch the current page URL.
Current version: 9.0 The context is an object that contains information about the user, culture, language settings, and database settings. You use the context, for example, to show the username or to set the language.
/// <summary>
/// Get the site info from the <see cref="SiteContextFactory"/> based on the item's path.
/// </summary>
/// <param name="item">The item.</param>
/// <returns>The <see cref="SiteInfo"/>.</returns>
public static SiteInfo GetSiteInfo(this Item item)
{
return SiteContextFactory.Sites
.Where(s => !string.IsNullOrWhiteSpace(s.RootPath) && item.Paths.Path.StartsWith(s.RootPath, StringComparison.OrdinalIgnoreCase))
.OrderByDescending(s => s.RootPath.Length)
.FirstOrDefault();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With