CRM 2011 – Getting the ServerUrl in Javascript and using

Whilst I was developing I had hard coded the serverUrl which I then used whilst doing an oData query.

So when I moved the code from the Dev system to production for the first time, my trusty Javascript stopped working.  I know you shouldn’t hard code stuff like this and I had only done it in development to get oData working.

The quick answer for those of you who have Googled your way to my blog, is you use the and use the method getServerUrl();  This will return you the url, including the organisation.  After that all you have to do is paste the oData address.

var serverUrl = Xrm.Page.context.getServerUrl();
var GlobalODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";

this is the code I used to do the oData call
function retrieveUserRecord(Id) {

var select = "/SystemUserSet?$select=InternalEMailAddress&$filter=SystemUserId eq guid'" + Id + "'";

    showMessage("retrieveUserRecord function START");
    var retrieveUserReq = new XMLHttpRequest();"GET", GlobalODataPath + select, true);
    retrieveUserReq.setRequestHeader("Accept", "application/json");
    retrieveUserReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    retrieveUserReq.onreadystatechange = function () {
    // showMessage("retrieveAccountRecord function END.");
    showMessage("retrieveAccountRecord function END.");

function retrieveUserReqCallBack(retrieveUserReq) {
    if (retrieveUserReq.readyState == 4 /* complete */) {
        if (retrieveUserReq.status == 200) {
            var retrievedUser = JSON.parse(retrieveUserReq.responseText).d;
            showMessage("ACTION: Retrieved email address = \"" + retrievedUser.results[0].InternalEMailAddress);
        else {
            showMessage("retrieveAccountReqCallBack function failure END");



Although the initial change from CRM 4 to CRM 2011 Javascript has a steep learning curve, once you are get used to it the CRM 2011 is I think better than CRM 4.  One of the cool new features is the object.  This is a object which Microsoft generate and fill with lots of lovely variables and methods.

It has three useful parts to it.  What I really like is the separation Microsoft has done here, it’s logical and easy to use.

Xrm.Page.context provides methods to retrieve information specific to an organization, a user, or parameters that were passed to the form in a query string provides an entity object that provides collections and methods to manage data within the entity form
Xrm.Page.ui provides collections and methods to manage the user interface of the form.
Out of the the three the one I use the most is and it has some great methods to get default/organisational information from.
  • getAuthenticationHeader: Returns the encoded SOAP header necessary to use Microsoft Dynamics CRM 4.0 Web service calls usingJScript.
  • getCurrentTheme Returns a string representing the current Microsoft Office Outlook theme chosen by the user.
  • getOrgLcid: Returns the LCID value that represents the Microsoft Dynamics CRM Language Pack that is the base language for the organization.
  • getOrgUniqueName: Returns the unique text value of the organizations name.
  • getQueryStringParameters: Returns an array of key value pairs representing the query string arguments that were passed to the page.
  • getServerUrl: Returns the base server URL. When a user is working offline with the Microsoft Dynamics CRM for Microsoft Office Outlook client, the URL is to the local Microsoft Dynamics CRM Web services.
  • getUserId: Returns GUID value of the value for the current user.
  • getUserLcid: Returns the LCID value that represents the Microsoft Dynamics CRM Language Pack that is the user selected as their preferred language.
  • getUserRoles: Returns an array of strings representing the GUID values of each of the security roles that the user is associated with.
  • isOutlookClient: Returns a Boolean value indicating if the user is using the Microsoft Dynamics CRM for Microsoft Office Outlook client.
  • isOutlookOnline: Returns a Boolean value indicating whether the user is connected to the Microsoft Dynamics CRM server while using the Microsoft Dynamics CRM for Microsoft Office Outlook with Offline Access client. When this function returns false, the user is working offline without a connection to the server. They are interacting with an instance of Microsoft Dynamics CRM running on their local computer.
  • prependOrgName: Prepends the organization name to the specified path.

12 thoughts on “CRM 2011 – Getting the ServerUrl in Javascript and using

  1. Jamie Miley May 26, 2011 / 7:45 pm

    hehe… you should have actually READ that jscript cheat sheet from Daniel Cai when you highlighted it in your blog back in April. :)

  2. Hosk May 26, 2011 / 8:05 pm

    that is the funny part, I had to find the cheat sheet by searching my own blog. I can never find it on my computer.

    Everything in the cloud

  3. Dan Rigby May 30, 2011 / 1:53 pm

    The one issue I’ve had with this is that if the CRM server is accessed via more then 1 host name (an edge case), you can get some weird issues with cross site scripting. (A lot of the common Silverlight CRM 2011 tools fail in this situation as well for the same reason.) I’ve ended up using relative paths for my OData calls in js as a result which avoids the issue.

    So instead of:

    var serverUrl = Xrm.Page.context.getServerUrl();
    var GlobalODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";

    I use:

    var orgPath = "/" + Xrm.Page.context.getOrgUniqueName();
    var oDataPath = orgPath + "/XRMServices/2011/OrganizationData.svc";

    • Michael Boland August 14, 2011 / 7:56 pm

      Thanks Dan,

      I just had our admin setup a FQN for the Dynamics URL. I couldn’t understand why any of our OData calls were failing…then I realized the server url issue after reading your post. Just made the change to the org name and everything is working great. Thanks a ton.

  4. Jenny Fer September 8, 2011 / 2:39 pm

    I got some really positive feedback, but some people were tripped up. Troubleshooting via blog is difficult, so whenever you have problems feel free to tweet me (@paul_way). Today, I’m going to go into great detail of changing the form again, but this time we’re going to add a new twist. Take a look at the before and after:

  5. suraj September 15, 2011 / 4:40 am

    Can we get the server time in the java script CRM2011

  6. mahesh May 14, 2012 / 10:15 am

    how genrate pdf to on save click event on online crm-2011

  7. rickard engström May 7, 2014 / 6:30 pm

    I almost never leave a response, but i did a few searching and
    wound up here CRM 2011 – Getting the ServerUrl in Javascript and using | Hosk’s Dynamic
    CRM Blog. And I actually do have some questions for you if you tend
    not to mind. Could it be only me or does it look as if like
    some of these comments look as if they are written by brain dead folks? :-P And, if you are posting on other sites, I’d like to follow anything fresh you have to post.
    Would you list of every one of your communal sites like your twitter feed,
    Facebook page or linkedin profile?

Leave a Reply

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

You are commenting using your 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