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
Very good tip.
Needing it:)
Thanks