Search This Blog

Tuesday, September 22, 2009

SiteMap, IFD/OnPremise and relative URLs

I don’t know if you've already faced this problem: When you develop a web page that you want displayed via the SiteMap Navigation, the URLs called are different depending whether you are in OnPremise or IFD mode.

In IFD, you get a URL like http://organization_name.domain.extension/ISV/default.aspx

In OnPremise, it will look like

I assume that you use the same URL to access both Dynamics CRM authentication mode.

Now, if you want to use relative URLs to load images, scripts or other (which are also contained in the ISV folder), you're facing a problem: how to use relative URLs to be valid for both IFD and OnPremise mode?

The simplest answer, which is often found on the forums, is: Use absolute URLs.

Certainly, but this is not very clean to use hard coded server names.

So I propose you my method. Maybe not the best but it does the job (even if, as we shall see, it still uses absolute URL).

In the code behind your ASP.Net page, create a new string property called BaseUrl.

In the Get accessor, write the following code:

public string BaseUrl
string baseUrl = string.Format("{0}://{1}",
Request.IsSecureConnection ? "https" : "http",

return baseUrl;

Then in the HTML content of your website, you can use the <% = BaseUrl %>wherever you want to reference relative URLs.

For example:

<img src="<%= BaseUrl %>/ISV/MyImages/Image.gif" > 

Well, it’s right, you still have an absolute URL in the source code of the page. But at least you do not have to worry about changing environments and authentication mode.

Note: If you need relative URLs on your scripts, you can replace the ".js" file by an ".aspx" file and perform the same manipulation as above.


delva2k4 said...

i don't understand why with the absolute URL we have to worry about changing environments.
Because we have the Orgname in parameter.

Like this :
sOrganisation = Request.QueryString["orgname"];
if (Request.Url.AbsoluteUri.Contains(sOrganisation))

delva2k4 said...

oh forget !

it's work in javascript too , with the "ORG_UNIQUE_NAME" and document.location object.

Tanguy said...

Well, some of my customers use both IFD and OnPremise accessing computer with DNS alias, so they always use an URL containing organization name...

In this case, you can't know if you are in IFD or OnPremise.

Moreover, it seems simpliest (at least to me) to use a property in code behind like I propose than replacing each potential control URL (image source, stylesheet and script file reference) in code behind or even in embedded javascript.