get item's relative url from another site in multisite architecture

In a multisite architecture, one of the component (in Site A) has a link to an item in another page (in Site B).

I am using LinkManager to get the url of the desired item by passing the desired item to GetItemUrl(). This is giving the absolute path including "/sitecore/content/TenantFolder/SiteName/...".

I am trying to get the relative path which does not include the /sitecore/content...

Any insights/suggestions will be greatly appreciated.

 

 

3 Replies

  • You need to resolve the site,

    This can be done with Sitecore.Links.LinkManager.ResolveTargetSite

    or
    private static SiteInfo GetSiteInfo(Item item)
    {
    return Sitecore.Sites.SiteContextFactory.Sites
    .Where(s => !string.IsNullOrWhiteSpace(s.RootPath) && item.Paths.Path.StartsWith(s.RootPath, StringComparison.OrdinalIgnoreCase))
    .OrderByDescending(s => s.RootPath.Length)
    .FirstOrDefault();
    }


    You can switch the Site Context with the SiteContextSwitcher

    using (new SiteContextSwitcher(SiteContextFactory.GetSiteContext("websiteAorB")))
    {
    //perform actions
    }

     

     

    See https://briancaos.wordpress.com/2012/03/28/sitecore-links-in-multisite-solutions-siteresolving/

  • In reply to Jan Bluemink:

    You can try to set the “targetHostName” for each site, it’s an attribute of site definition.
  • var options = LinkManager.GetDefaultUrlOptions();
    options.ShortenUrls = true;
    options.SiteResolving = true;
    linkUrl = LinkManager.GetItemUrl(selectedItem, options);

    This resolved the issue.