Getting the CRM url for Silverlight based on the browser

When creating Silverlight applications the normal method of retrieving the url for the CRM server that will be used to connect to the REST endpoint is usually based on the CRM url that is stored in the CRM config database and retrieved based on Xrm.Page.context.getServerUrl().

There can often be problems with using this address, for instance in development environments or similar, as the address returned might not be the address used in Internet Explorer. When this happens, Internet Explorer will block the connection to the REST endpoint since it does not connect to the same URL as the webpage that hosts the Silverlight application.

During the latest course I held for some CRM consultants at Evry, we discussed this and one of the attendees, Klas Adolfsson at Evry, was very quick and wrote an alternative to the getServerUrl()-method that bases the url on the hosting browser url instead of the url context variable from CRM.

HtmlPage.Document.DocumentUri.Scheme + “://” + HtmlPage.Document.DocumentUri.Host + “/” + App.Current.Host.InitParams[“orgname”];

Edit: There are some relevant comments to this posting below, please read these if this posting is of interest to you.

Gustaf Westerlund
CEO, Chief Architect and co-Founder at CRM-konsulterna AB
www.crmkonsulterna.se