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

In Sitecore the method LinkManager.GetItemUrl() can be used to retrieve the url of a certain content item. This call may however produce incorrect results when used by a scheduled task.

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/Home/Portal".
- International site with root item (i.e. homepage) "/sitecore/content/Home/International".

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

  <site
       name="WWWPortal"
       hostName="www.mysite.nl"
       rootPath="/sitecore/content/Home"
       startItem="/Portal"
       contentStartItem="/Portal"
       />

  <site
       name="WWWInternational"
       hostName="www.international.mysite.nl"
       rootPath="/sitecore/content/Home"
       startItem="/International"
       contentStartItem="/International"
       />

Now suppose you have written some code to export data and the url of the content item is a part of the exported data. This code uses the following code snippet for generating the url of a content item:

Sitecore.Links.UrlOptions SitecoreUrlOptions = new Sitecore.Links.UrlOptions();
SitecoreUrlOptions.AddAspxExtension = true;
SitecoreUrlOptions.LanguageEmbedding = Sitecore.Links.LanguageEmbedding.Never;
SitecoreUrlOptions.ShortenUrls = true;
SitecoreUrlOptions.SiteResolving = true;

String url = Sitecore.Links.LinkManager.GetItemUrl(contentItem, SitecoreUrlOptions);

This code has been implemented as a scheduled task, but for ease of use it has also been implemented as a command in the sitecore menu.

Now when you run this code from the sitecore menu the resulting url’s are correct, for example:

http:// www.mysite.nl/page1.aspx

However, when this code is executed by the scheduled task, the resulting url’s are incorrect and look like this:

:// www.mysite.nl/page1.aspx

For some reason the protocol (in this case “http”) is missing!

It is not really clear why this is happening. It is properly caused by one of the following reasons:
- A scheduled task does not run in the context of a http request.
- A scheduled task runs in the context of the site “scheduler”.

But fortunately there is a simple solution for this problem. The solution is to add the attribute "scheme" with the value "http" to the definition of your sites:

  <site
       name="WWWPortal"
       hostName="www.mysite.nl"
       scheme="http"
       rootPath="/sitecore/content/Home"
       startItem="/Portal"
       contentStartItem="/Portal"
       />

  <site
       name="WWWInternational"
       hostName="www.international.mysite.nl"
       scheme="http"
       rootPath="/sitecore/content/Home"
       startItem="/International"
       contentStartItem="/International"
       />

For some reason the attribute “scheme” is not documented! The comments in the web.config in front of the sites element do not mention this attribute. And on the sdn website the attributes of the site element are described here, but the attribute "scheme" is not mentioned here as well.

 © Evident Interactive BV