4

I've multisite solution setup, and sites config looks like below:

<sites>
  <site name="localhost" hostName="local.com" scheme="http" notFoundItem="not-found" enableTracking="true" virtualFolder="/product" physicalFolder="/website" targetHostName="local.com" rootPath="/sitecore/content/local" startItem="/login" database="web" domain="extranet" allowDebug="true" cacheHtml="false" filteredItemsCacheSize="10MB" enablePreview="true" enableWebEdit="true" enableDebugger="true" disableClientData="false"/>
  <site name="localhostaccount" hostName="localaccount.com" scheme="http" notFoundItem="not-found" enableTracking="true" virtualFolder="/product" physicalFolder="/website" targetHostName="localaccount.com" rootPath="/sitecore/content/localaccount" startItem="/login" database="web" domain="extranet" allowDebug="true" cacheHtml="false" filteredItemsCacheSize="10MB" enablePreview="true" enableWebEdit="true" enableDebugger="true" disableClientData="false"/>
  <site name="website" enableTracking="true" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/content" startItem="/home" database="web" domain="extranet" allowDebug="true" cacheHtml="true" htmlCacheSize="50MB" registryCacheSize="0" viewStateCacheSize="0" xslCacheSize="25MB" filteredItemsCacheSize="10MB" enablePreview="true" enableWebEdit="true" enableDebugger="true" disableClientData="false" cacheRenderingParameters="true" renderingParametersCacheSize="10MB" patch:source="Sitecore.LanguageFallback.config" language="en" itemwebapi.mode="Off" itemwebapi.access="ReadOnly" itemwebapi.allowanonymousaccess="false" enableItemLanguageFallback="false" enableFieldLanguageFallback="false"/>
  <site name="scheduler" enableTracking="false" domain="sitecore" patch:source="Sitecore.config"/>
  <site name="system" enableTracking="false" domain="sitecore" patch:source="Sitecore.config"/>
  <site name="publisher" domain="sitecore" enableTracking="false" enableWorkflow="true" patch:source="Sitecore.config"/>
</sites>

When I add links of one site in another, the hostname is not resolved in the url. Necessary site resolving config is done. Am I missing anything here?

-->
<setting name="Rendering.SiteResolving" value="true" />
<!--  RENDERING - SITE RESOLVING MATCH CURRENT LANGUAGE
            Affects how cross-site links are rendered when Rendering.SiteResolving is enabled.
            If true, the link provider will take the language attribute of the site definitions into consideration when resolving
            which site/hostname to use when rendering a cross-site link.
            Default value: true
      -->
<setting name="Rendering.SiteResolvingMatchCurrentLanguage" value="true" />
<!--  RENDERING - SITE RESOLVING MATCH CURRENT SITE
            Affects how cross-site links are rendered when Rendering.SiteResolving is enabled.
            If true, the link provider will check if the target item is located under the start item for the current site before it
            tries to find a match in the full list of site definitions. This ensures that when the target item can be resolved using
            the current site, the target link will not change to a different site/hostname.
            Default value: true
      -->
<setting name="Rendering.SiteResolvingMatchCurrentSite" value="true" />
<!--  USE SERVER-SIDE REDIRECT FOR REQUEST ERRORS
            If true, Sitecore will use Server.Transfer instead of Response.Redirect to redirect request to service pages
            when an error occurs (item not found, access denied etc).
            Default value: false
      -->
  • 1
    How are you adding linking of one site in another? Could you be more concrete? – Peter Procházka May 9 '18 at 10:54
  • 1
    I think he is picking an item from another site, in the editor, and expecting it to resolve the URL for that site. I don't think Sitecore does that out of the box. I always had to create a custom site resolver. – Chris Auer May 9 '18 at 14:30
  • @ChrisAuer .. thats correct I was thinking that Sitecore handles that until I came across this blog sitecorepm.wordpress.com/2010/08/04/… – nari447 May 10 '18 at 10:37
4

I've had to add urlOptions.SiteResolving = true in my LinkProvider override and my GetItemUrl extension to get this to work properly. This is in Sitecore 8.2 and older versions, not sure about Sitecore 9.

0

I think this should work for you. I used it for bucketed items, and shortening the urls. But this will determine in the current item is in the current site. Else it will find the proper site and resolve the link using that site. I have not tested this code in this shape.

public class MultisiteItemLinkProvider : LinkProvider
{
    public override void Initialize(string name, NameValueCollection config)
    {
        Assert.ArgumentNotNullOrEmpty(name, "name");
        Assert.ArgumentNotNull(config, "config");
        base.Initialize(name, config);
    }

    public override string GetItemUrl(Item item, UrlOptions 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, UrlOptions options)
    {
        if (IsFromCurrentSite(item)) return base.GetItemUrl(item, options);

        var website = this.GetSiteContext(item); //Sitecore.Configuration.Factory.GetSite("usms");
        using (new SiteContextSwitcher(website))
        {
            options.AlwaysIncludeServerUrl = true;
            options.SiteResolving = true;
            return base.GetItemUrl(item, options);
        }
    }

    private static List<KeyValuePair<string, SiteContext>> GetSites()
    {
        return SiteManager.GetSites()
            .Where(
                s =>
                    !string.IsNullOrEmpty(s.Properties["rootPath"]) &&
                    !string.IsNullOrEmpty(s.Properties["startItem"]))
            .Select(
                d => new KeyValuePair<string, SiteContext>($"{d.Properties["rootPath"]}{d.Properties["startItem"]}",
                    new SiteContext(new SiteInfo(d.Properties))))
            .ToList();
    }

    public virtual SiteContext GetSiteContext(Item item)
    {

        var site = GetSites().LastOrDefault(s => item.Paths.FullPath.ToLower().StartsWith(s.Key.ToLower()));
        return site.Value;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.