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

Silverlight and Dynamics CRM

Silverlight is a new product from Microsoft that can be viewed as the same sort of plug-in as for instance Flash, Java Applets or ActiveX. There are two feautres that make it stand out from the rest, namely:
– Vector based graphics built on the new Windows Presentation Foundation (a part of .NET 3.0). The definition of the GUI is definied in a langauge called XAML.
– From Silverlight version 1.1, you can write managed code. In other word you can finally start working with C# or VB.NET on the client side instead of the irritating language javascript.

These two features make the foundation for a more user friendly and more rich experience of using the web, something at least I have been longing for. Being able to use managed code/CLR-code, will make web development a lot easier and speed up the process of making more robust and reusable programs.

On the CRM Team Blog you can download an example of how silverlight can be used in conjunction with Dynamics CRM 3. I saw that some people had had some problems with it, and I havn’t had time to test it myself yet, so be advised.

Here is the link: http://blogs.msdn.com/crm/archive/2007/06/19/crm-and-silverlight-integration.aspx

Gustaf Westerlund
CRM and SharePoint Consultant

Humandata AB
www.humandata.se