Home

Thursday, September 14, 2017

Retrieve an entity using Dynamics 365 Web API


The following example retrieves name and revenue properties for the account entity with the Id

Request
GET [Organization URI]/api/data/v8.2/accounts(00000000-0000-0000-0000-000000000001)?$select=name,revenue HTTP/1.1
Accept: application/json
Content-Type: application/json; charset=utf-8
OData-MaxVersion: 4.0
OData-Version: 4.0

Response
HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0

{
"@odata.context": "[Organization URI]/api/data/v8.2/$metadata#accounts(name,revenue)/$entity",
"@odata.etag": "W/\"502186\"",
"name": "A. Datum Corporation (sample)",
"revenue": 10000,
"accountid": "00000000-0000-0000-0000-000000000001",
"_transactioncurrencyid_value":"b2a6b689-9a39-e611-80d2-00155db44581"
}
------------------------------------------------------------------------------------------------------
function GetRecord() {
   
    var entityName = "accounts", etag;
    var clientURL = Xrm.Page.context.getClientUrl();
    var req = new XMLHttpRequest();

    req.open("GET", encodeURI(clientURL + "/api/data/v8.2/" + entityName + "(00000000-0E99-E711-8127-000000000001)?$select=name,revenue", false));
    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) {
                var result = JSON.parse(this.response);               
            }
            else {
                var error = JSON.parse(this.response).error;
                alert(error.message);
            }
        }
    };

    req.send();
  

}

No comments:

Post a Comment

Convert subgrid to Comments