Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive

Sitecore: LinkManager.GetItemUrl and wildcards

Modified: 2010/05/10 11:48 by vanthoog - Categorized as: Sitecore
In Sitecore the method LinkManager.GetItemUrl() can be used to retrieve the url of a certain item. This call may however produce incorrect results when wildcards are used in a site definition.

Suppose you have set up Sitecore to support multiple websites and two sites have been created:
- Portal site with root item (i.e. homepage) “/sitecore/content/Mobile/Portal”.
- International site with root item (i.e. homepage) “/sitecore/content/Mobile/International”.

The definition of both sites in the web.config look like this (note: attributes irrelevant for this article have been removed):

  <site
       name="MobilePortal"
       hostName="m.mysite.local"
       rootPath="/sitecore/content/Mobile"
       startItem="/Portal"
       contentStartItem="/Portal"
       />

  <site
       name="MobileInternational"
       hostName="m.international.mysite.local"
       rootPath="/sitecore/content/Mobile"
       startItem="/International"
       contentStartItem="/International"
       />

Notice how both sites have been set up to be accessible via only a single url (i.e. no wildcards have been used).

In the code of both sites you can use the following code snippet for retrieving the url to the homepage of the portal site:

Item portalItem = ItemManager.GetItem("/sitecore/content/Mobile/Portal",
   Sitecore.Globalization.Language.Parse("nl"),
   Sitecore.Data.Version.Latest,
   Sitecore.Context.Database);

UrlOptions options = new UrlOptions { 
   SiteResolving = true, 
   LanguageEmbedding = LanguageEmbedding.Never 
   };

string portalUrl = LinkManager.GetItemUrl(portalItem, options);

This code snippet will always produce the value “http://m.mysite.local”. Or in other words, the variable portalUrl will always contain the value “http://m.mysite.local”.

Now suppose that the portal site must also be reachable via the url http://m.mysite2.local. According to SDN you can use wildcards in the attribute hostname of a site definition. So you could achieve this by adding a wildcard to the hostName attribute of site definition of the portal site:

  <site
       name="MobilePortal"
       hostName="m.mysite*.local"
       rootPath="/sitecore/content/Mobile"
       startItem="/Portal"
       contentStartItem="/Portal"
       />

This will indeed work. So the portal site can now be accessed via http://m.mysite2.local. But the disadvantage of this approach is that the GetItemUrl code snippet will produce incorrect results in some situations.

When used in the portal site, the code snippet will produce correct results, being the url http://m.mysite.local or http://m.mysite2.local depending on which url has been used to access the site. However in the international site the result will be http://m.international.mysite.local/Mobile/Portal and this is not correct and will actually result in a “page not found” error (http error status 404).

At this moment we do not know of a proper solution for this problem. There is of course a simple workaround, being not to use wildcards but to add a second site definition for the portal site:

  <site
       name="MobilePortal"
       hostName="m.mysite.local"
       rootPath="/sitecore/content/Mobile"
       startItem="/Portal"
       contentStartItem="/Portal"
       />

  <site
       name="MobilePortal2"
       hostName="m.mysite2.local"
       rootPath="/sitecore/content/Mobile"
       startItem="/Portal"
       contentStartItem="/Portal"
       />

In this case the GetItemUrl code snippet will return the url of the first site definition, being m.mysite.local. So the sequence of the site definitions in the web.config is important.

 © Evident Interactive BV