You only have an instance of Item.
How do you get it's "official" site, assuming there are multiple Sites defined in configuration?
Sitecore Stack Exchange is a question and answer site for developers and end users of the Sitecore CMS and multichannel marketing software. It only takes a minute to sign up.Sign up to join this community
It's possible to determine the
Site an item "belongs" to using the LinkManager. This is how Sitecore resolves links internally when
Rendering.SiteResolving = true and expanding links or resolving the Preview Site when
Preview.ResolveSite = true.
var item = Sitecore.Context.Database.GetItem("/sitecore/content/my-item"); SiteInfo site = Sitecore.Links.LinkManager.ResolveTargetSite(item);
Possibly not the best use of the
LinkManager and it feels a little dirty as a result, but this mean that you follow the same logic as Sitecore and should something change in the future then your own code will follow that same logic.
Probably the most terse:
string itemPath = item.Paths.FullPath; SiteInfo site = SiteContextFactory.Sites .Where (s => s.RootPath !="" & itemPath.StartsWith(s.RootPath, StringComparison.OrdinalIgnoreCase)) .OrderByDescending(s => s.RootPath.Length) .FirstOrDefault();
itemPath is factored out of the LINQ for performance.