Tuesday, March 26, 2013

CRM 2011 Get the right server url in javascript

I used the method found on Daniel Cai's Blog and it worked for me, hopefully it will for for others too.

My function is a little modified (I removed some checks) but uses the same idea from Daniel's blog. Also the idea with prependOrgName that Andrii Butenko suggested worked for me.

My function looks like this:


function GetServerUrl () {

    var context = Xrm.Page.context;

    if (context.isOutlookClient() && !context.isOutlookOnline()) {
        crmServerUrl = window.location.protocol + "//" + window.location.host;
    } else {
        crmServerUrl = context.getServerUrl();
        crmServerUrl = crmServerUrl.replace(/^(http|https):\/\/([_a-zA-Z0-9\-\.]+)(:([0-9]{1,5}))?/, window.location.protocol + "//" + window.location.host);
        crmServerUrl = crmServerUrl.replace(/\/$/, ""); // remove trailing slash if any
    }
    return crmServerUrl;
}

Update: With the Rollup 12 we can use getClientUrl that will solve all our problems. See bellow description taken from msdn


getClientUrl

Returns the base URL that was used to access the application.
This method is new in Microsoft Dynamics CRM 2011 Update Rollup 12 and the Microsoft Dynamics CRM December 2012 Service Update.
The values returned will resemble those listed in the following table.

 

ClientValue
Microsoft Dynamics CRM (on-premises)
http(s)://server/org
Microsoft Dynamics CRM Online
https://org.crm.dynamics.com
Microsoft Dynamics CRM for Outlook with Offline Access when offline
http://localhost:2525
Return Value
Type: String

No comments:

Post a Comment