Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out with which Sitecore site an item is associated?

Tags:

sitecore

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.

like image 764
wildwend Avatar asked Jan 07 '13 17:01

wildwend


People also ask

How do I find my website URL on Sitecore?

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.

What is Sitecore context?

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.


1 Answers

/// <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();
}
like image 52
BraveNewMath Avatar answered Nov 14 '22 11:11

BraveNewMath