CRM 2011 – Alternative to context.getServerUrl()


As all of you know context.getServerURL, returns the base server URL. The format of this Url can change depending on whether the user is connected to Microsoft Dynamics CRM 2011 On Premises, Microsoft Dynamics CRM Online, or working offline with Microsoft Dynamics CRM for Microsoft Office Outlook with Offline Access.

However there is one constrain using context.getServerURL as given in SDK Help –

The URL returned always returns the standard URL used to access the application. If you accessing the server locally using http://localhost or are using an IP address rather than the actual name of the server this will not be reflected in the value returned by this function. This means that if you are making web service calls or accessing Web resources Internet Explorer will apply security settings that apply to requests that cross domains. To avoid this, always connect to Microsoft Dynamics CRM using the standard URL.

So if user is accessing CRM using IP address, any call made to CRM using REST endpoint or SOAP endpoint will throw “Access Denied” error.

I have adopted following workaround for this problem which let user use ServerName as well as IP address for accessing CRM

Note: This approach does not work in CRM Online, you might have to tweek in little bit to get it work in CRM Online

Basically I am by passing context.getServerURL call and constructing ServerURL using Document.location property

var customServerURL = document.location.protocol + “//” + document.location.host + “/” + Xrm.Page.context.getOrgUniqueName();

For Silverlight Web Resources it is

stringserverUrl = string.Empty;
string orgUniqueName = string.Empty;
orgUniqueName = (string)GetContext().Invoke(“getOrgUniqueName”);

if (HtmlPage.Document.DocumentUri.Port != 80 && HtmlPage.Document.DocumentUri.Port != -1)
{
serverUrl =string.Format(“{0}://{1}:{2}/{3}”, HtmlPage.Document.DocumentUri.Scheme, HtmlPage.Document.DocumentUri.Host, HtmlPage.Document.DocumentUri.Port.ToString(), orgUniqueName);
}
else
{
serverUrl =string.Format(“{0}://{1}/{2}”, HtmlPage.Document.DocumentUri.Scheme, HtmlPage.Document.DocumentUri.Host, orgUniqueName);
}

Hope this helps

Advertisements

5 thoughts on “CRM 2011 – Alternative to context.getServerUrl()

  1. Hi all,
    Just a quick comment for anyone still looking at this. As of Roll Up 12 getServerUrl is deprecated and replaced with getClientUrl which works for all scenarios (so Microsoft claim)

    Chris

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s