Monday, March 25, 2013

Query CRM 2011 data with JSON synchronously

After we installed CRM update rollup 12 (KB 2795627), we encountered very big problems with our customizations relating to SOAP queries that were across multiple entities. CRM rollup 12 (finally) introduces us with additional browser compatibility for Firefox, Chrome, and Safari.

After rollup our customizations that were using SOAP requests didn't work anymore on Chrome, Firefox and Internet Explorer 10. We discovered that SOAP requests uses ActiveXobject and this is not supported!!!

To rebuild our customizations we used synchronous JSON queries, that solved our problems and even made our crm customizations better and whole scripting technique lighter.

This excellent article  Read records synchronously using JSON in CRM 2011 helped us to build our JSON queries like this:

Example function for retrieving current user full name

function getUserFullName() {

var userid = Xrm.Page.context.getUserId();


var retrieveRecordsReq = new XMLHttpRequest();
  var ODataPath = "/xrmservices/2011/OrganizationData.svc/SystemUserSet?$select=FullName&$filter=SystemUserId eq guid'" + userid + "'";
retrieveRecordsReq.open('GET', ODataPath, false);
retrieveRecordsReq.setRequestHeader("Accept", "application/json");
retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
retrieveRecordsReq.send(null);


var records = JSON.parse(retrieveRecordsReq.responseText).d;
var FullName = records.results[0].FullName;
return FullName;

}