With the release of Dynamics CRM 2016, organization data services is depreciated, so going forward we need to make sure to write scripting code using Web API only. Web API implements OData V4 and can be used for any operation that we can do using organization service, so now we can use Web API to get both data and metadata. In this article, we are providing retrieve method sample code
To use Web API we need to use URL like the following:
[Organization URI] +”/api/data/v8.0/”
- function retrieveEntity(entityname,id, columnset) {
- var serverURL = Xrm.Page.context.getClientUrl();
- var Query = entityname+ "(" + id + ")" + columnset;
- var req = new XMLHttpRequest();
- req.open("GET", serverURL + "/api/data/v8.0/" + Query, true);
- req.setRequestHeader("Accept", "application/json");
- req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
- req.setRequestHeader("OData-MaxVersion", "4.0");
- req.setRequestHeader("OData-Version", "4.0");
- req.onreadystatechange = function() {
- if (this.readyState == 4 /* complete */ ) {
- req.onreadystatechange = null;
- if (this.status == 200)//to verify result is OK {
- var data = JSON.parse(this.response);
- if(data!=null && data.accountnumber!=null)
- alert(data.accountnumber);
- } else {
- var error = JSON.parse(this.response).error;
- alert(error.message);
- }
- }
- };
- req.send();
- }
- var Id=entityid; //primary key (GUID) , make sure you are passing GUID without {}
- var entityName="accounts";
- var columnSet="?$select=accountnumber,address1_city"; //list of column that we want to fetch
- retrieveEntity(entityName,Id,columnSet);
This code can be modified to work with any entity and to bring attributes based on requirement.
Stay tuned for more Web API Samples!







Firoz ctPosted Nov 4, 2016, 3:45 AM EDT
Thanks for the article but it doest work for me.. Getting this error - One of the scripts for this record has caused an error. For more details, download the log file.ReferenceError: entityname is not defined at retrieveEntityNew (https://xfactor203.crm8.dynamics.com/%7B636138565930000526%7D/WebResources/new_webAPIConnect?ver=-1133214757:51:2
Abhishek SinhaPosted Apr 29, 2016, 7:11 AM EDT
Code also doesn't work
Abhishek SinhaPosted Apr 29, 2016, 6:49 AM EDT
It doesn't work we need to use slice function to remove curly braces of the guid .http://stackoverflow.com/questions/36192845/dynamics-crm-2016-web-api-query-dynamic-values.
Nitin ResPosted Feb 5, 2016, 6:55 AM EST
Thanks for sharing.....I want to use this CRM Web API in console application. So can you help me out how do I get connected to my CRM online instance? (i.e authenticating it with credentials and use the web api).
Gowtham KTOP 100Posted Jan 5, 2016, 6:50 AM EST
Good One, Thanks for sharing
Gowtham RajamanickamTOP 100Posted Jan 5, 2016, 2:22 AM EST
Good article
Debasis SahaTOP 100Posted Jan 4, 2016, 11:57 PM EST
Good one..
Humayun Kabir MamunPosted Jan 4, 2016, 10:47 PM EST
Nice...
Humayun Kabir MamunPosted Dec 28, 2015, 6:13 PM EST
Nice...
Santhakumar MunuswamyPosted Dec 28, 2015, 8:20 AM EST
Thanks for nice sharing