Using cross site links.. aka dynamic links (part 1)

When you have multiple sites defined in Sitecore and you use LinkManager to get the URL to items in your site, all is well. Trouble arises however when you want a link to an item outside of the context site. For example, if you have a sitecore content tree and site config like:

  • /sitecore/content/Home
    • SiteA
      • ItemA1
    • SiteB
      • ItemB1

If you had a custom control on SiteA that used LinkManager.GetItemURL(ItemB1), the return URL would be something like:

www.SiteA.com/sitecore/content/home/SiteB/ItemB1

when what you really want is:

www.SiteB.com/ItemB1

Several people in my group searched high and low for a Sitecore configuration that would result in exactly that. After much research, it seemed that the undocumented “SiteResolving” setting is what we needed.

To get SiteResolving to work, you need to:

  1. Set the targetHostName on the desired site config entries
  2. When calling LinkManager.GetItemUrl, pass in UrlOptions with UrlOptions.SiteResolving=true.

It seems that if targetHostName is set and UrlOptions.SiteResolving=true, then when a GetItemUrl request is made to the LinkProvider it will check whether the requested item is a descendant of the context site. If not, then the SiteResolving logic kicks in. This logic takes the requested item path and recursively chops off the last part of the path until a match is found. The match is against the site config’s RootPath+StartItem. If a match is found, that site’s targetHostName is used when generating the URL.

However, the trickiness is really setting UrlOptions.SiteResolving to true, unless you are going to remember to explicitly set this each time you call LinkManager.GetItemUrl. You would think adding the following web.config setting would do the trick:

<setting name=”Rendering.SiteResolving” value=”true” />

But the neither the LinkProvider code nor the default UrlOptions seem to use this. So, in order to get this to work, I just added the following code to my custom LinkProvider:


public class LinkProvider : Sitecore.Links.LinkProvider

{

public override string GetItemUrl(Item item, UrlOptions options)

{

string itemUrl;

//other code

options.SiteResolving = Sitecore.Configuration.Settings.Rendering.SiteResolving;

itemUrl = base.GetItemUrl(item, options);

//other code

return itemUrl;

}

}

}

Thanks to Mike Casey for helping find this solution.

About these ads

About Paul Martin

I enjoy rock climbing, playing guitar, writing code...
This entry was posted in Dynamic Links, LinkManager. Bookmark the permalink.

4 Responses to Using cross site links.. aka dynamic links (part 1)

  1. Pingback: Sitecore links in multisite solutions – SiteResolving « Brian Pedersen’s Sitecore and .NET Blog

  2. Pingback: Sitecore Links with LinkManager and MediaManager | Brian Pedersen's Sitecore and .NET Blog

  3. Pingback: Sitecore Links with LinkManager and MediaManager | CMS News Today

  4. GFoley83 says:

    Very helpful. Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s