CRM 2011 – Getting the ServerUrl in Javascript and using Xrm.page

Posted on May 26, 2011

10


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 Xrm.page.context 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 + "'";

    ///SystemUserSet?(guid'4d82f1fc-262e-e011-9645-00155d106b02')
    showMessage("retrieveUserRecord function START");
    var retrieveUserReq = new XMLHttpRequest();
    retrieveUserReq.open("GET", GlobalODataPath + select, true);
    retrieveUserReq.setRequestHeader("Accept", "application/json");
    retrieveUserReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    retrieveUserReq.onreadystatechange = function () {
        retrieveUserReqCallBack(this);
    };
    retrieveUserReq.send();
    // showMessage("retrieveAccountRecord function END.");
    showMessage("retrieveAccountRecord function END.");
}

function retrieveUserReqCallBack(retrieveUserReq) {
    if (retrieveUserReq.readyState == 4 /* complete */) {
        if (retrieveUserReq.status == 200) {
            //Success
            var retrievedUser = JSON.parse(retrieveUserReq.responseText).d;
            showMessage("ACTION: Retrieved email address = \"" + retrievedUser.results[0].InternalEMailAddress);
            setValuesIFrameQueryString(retrievedUser.results[0].InternalEMailAddress);
        }
        else {
            //Failure
            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 Xrm.page 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
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
Xrm.Page.data.entity
Xrm.Page.data provides an entity object that provides collections and methods to manage data within the entity form
Xrm.Page.ui
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 Xrm.page 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 SystemUser.id 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.
About these ads