Translate

Tuesday, 5 November 2013

Retrieve data in crm 2013

Replacement of SOAP in CRM for retrieve data with java script.

Hello CRM Lovers,
    First of all thanks for reading my post. As we all know that after CRM Roll-Up 12, SOAP has stopped working for cross browser support in CRM, so we have to write some other code to solve this problem. I found a way to solve this problem so I am sharing it with the beautiful world.
 
    In this post I am going to describe how to retrieve data with java script of another entity in CRM 2011, CRM 2013 with JSON and OData.It will work on Roll-Up 12, Roll-Up 13 for CRM 2011 and on CRM 2013, and also in cross browsers. It will really help you. For retrieving data please fallow these steps.

  Retrieving Multiple Records with ODATA in CRM with Java Script

  Java script for retrieving data in cross browser

Step 1:-  First of all you have to add this function in your web resource.

//***********************************************************************************************************************
 //*******Retrive Multiple recored on the basis of the criteria OData Rest //******************************************************************************************

function retrieveMultiple(odataSetName, filter, successCallback, errorCallback, _executionObj) {
    _executionObjMultiretrive = _executionObj;
    var context = Xrm.Page.context;
    var serverUrl = context.getServerUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    //odataSetName is required, i.e. "AccountSet"  
    if (!odataSetName) {
        alert("odataSetName is required.");
        return;
    }
    //Build the URI 
    var odataUri = serverUrl + ODATA_ENDPOINT + "/" + odataSetName;
    //If a filter is supplied, append it to the OData URI
    if (filter) {
        odataUri += filter;
    }
    //Asynchronous AJAX function to Retrieve CRM records using OData
    $.ajax({
        type: "GET",
        async: false,
        contentType: "application/json; charset=utf-8",
        datatype: "json", url: odataUri,
        beforeSend: function (XMLHttpRequest) {
            //Specifying this header ensures that the results will be returned as JSON.
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        success: function (data, textStatus, XmlHttpRequest) {
            if (successCallback) {
                if (data && data.d && data.d.results) {
                    successCallback(data.d.results, textStatus, XmlHttpRequest);
                }
                else if (data && data.d) {
                    successCallback(data.d, textStatus, XmlHttpRequest);
                }
                else {
                    successCallback(data, textStatus, XmlHttpRequest);
                }
            }
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) {
            if (errorCallback)
                errorCallback(XmlHttpRequest, textStatus, errorThrown);
            else
                errorHandler(XmlHttpRequest, textStatus, errorThrown);
        }
    });
}
function errorHandler(xmlHttpRequest, textStatus, errorThrow) {
    alert("Error : " + textStatus + ": " + xmlHttpRequest.statusText);
}


Step 2 :-  Now add the fallowing functions with your custom code. Suppose you want to retrieve all the records from a entity which has single line text attribute and its value = "multiple retrieve with odata". for this condition you can use this code.

//**********************************************************************************//*****************Your custom function for retrieving data********************
//**********************************************************************************
    function YourFunction() {
        var _condition= "multiple retrieve with odata";
       if (_condition!= null) {
             retrieveMultiple("Entity NameSet", "?$filter=Attribute Schema Name eq '"+_condition+"' ", RetrieveEntityRecords, null, null);
        }
    }

//****************************************************************//****************************************************************

    function RetrieveEntityRecords(data, textStatus, XmlHttpRequest) {
        //debugger;      
      if (data.length > 0) {
             for (i = 0; i < data.length; i++) {
                var attribute1 = data[i].attribute1 schema name;
                var attribute2 = data[i].attribute2 schema name;
           }
        }
    }

Here  "data" is object of all retrieved records, you can find  your required attribute accordingly.


I know your condition may be differ.

The filter condition differs on the basis of attribute type so you can visit this link for more conditions...

http://msdn.microsoft.com/en-us/library/gg309461.aspx

http://msdn.microsoft.com/en-us/library/hh169248%28v=nav.71%29.aspx

Or you can download odata query designer from this link from here you will get a managed solution import it in your organization and design your odata query according to your requirement. 

http://crm2011odatatool.codeplex.com/


So by fallowing all upper steps you can retrieve records . It will work in cross browser and in all new Roll-Up 12,Roll-Up 13 . 

Enjoy the great technology Dynamics CRM. Good luck.....!!!!! 
Thanks.
Post a Comment