Node.js Dynamics CRM: Get an Account's Contacts

Back to Index

Returns the full name, email address, and phone number for each of an account's contacts. This example gets the contacts for the Coho Winery account (accountid = b0a19cdd-88df-e311-b8e5-6c3be5a8b200).

Documentation: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/web-api/contact?view=dynamics-ce-odata-9

CURL Command

curl -X GET https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/contacts \
  -H "Accept: application/json" \
  -H "OData-MaxVersion: 4.0"  \
  -H "OData-Version: 4.0" \
  -d "$select=fullname,emailaddress1,telephone1" \
  -d "$filter=_parentcustomerid_value eq b0a19cdd-88df-e311-b8e5-6c3be5a8b200" \
  -H "Authorization: Bearer DYNAMICS_CRM_ACCESS_TOKEN"

Node.js Example

var os = require('os');
if (os.platform() == 'win32') {  
    var chilkat = require('chilkat_node6_win32'); 
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('chilkat_node6_arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('chilkat_node6_linux32');
    } else {
        var chilkat = require('chilkat_node6_linux64');
    }
} else if (os.platform() == 'darwin') {
    var chilkat = require('chilkat_node6_macosx');
}

function chilkatExample() {

    var rest = new chilkat.Rest();
    var success;

    //  URL: https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/contacts
    var bTls = true;
    var port = 443;
    var bAutoReconnect = true;
    success = rest.Connect("my-dynamics-domain.api.crm.dynamics.com",port,bTls,bAutoReconnect);
    if (success !== true) {
        console.log("ConnectFailReason: " + rest.ConnectFailReason);
        console.log(rest.LastErrorText);
        return;
    }

    rest.AddQueryParam("$select","fullname,emailaddress1,telephone1");
    rest.AddQueryParam("$filter","_parentcustomerid_value eq b0a19cdd-88df-e311-b8e5-6c3be5a8b200");

    rest.AddHeader("OData-MaxVersion","4.0");
    rest.AddHeader("Accept","application/json");
    rest.AddHeader("OData-Version","4.0");
    rest.AddHeader("Authorization","Bearer DYNAMICS_CRM_ACCESS_TOKEN");

    var sbResponseBody = new chilkat.StringBuilder();
    success = rest.FullRequestNoBodySb("GET","/api/data/v9.0/contacts",sbResponseBody);
    if (success !== true) {
        console.log(rest.LastErrorText);
        return;
    }

    var respStatusCode = rest.ResponseStatusCode;
    if (respStatusCode >= 400) {
        console.log("Response Status Code = " + respStatusCode);
        console.log("Response Header:");
        console.log(rest.ResponseHeader);
        console.log("Response Body:");
        console.log(sbResponseBody.GetAsString());
        return;
    }

    var jsonResponse = new chilkat.JsonObject();
    jsonResponse.LoadSb(sbResponseBody);

    var i;
    var count_i;

    var odataContext = jsonResponse.StringOf("\"@odata.context\"");
    i = 0;
    count_i = jsonResponse.SizeOfArray("value");
    while (i < count_i) {
        jsonResponse.I = i;
        var odataEtag = jsonResponse.StringOf("value[i].\"@odata.etag\"");
        var fullname = jsonResponse.StringOf("value[i].fullname");
        var emailaddress1 = jsonResponse.StringOf("value[i].emailaddress1");
        var telephone1 = jsonResponse.StringOf("value[i].telephone1");
        var contactid = jsonResponse.StringOf("value[i].contactid");
        i = i+1;
    }


}

chilkatExample();

Sample JSON Response Body

{
  "@odata.context": "https://mydomain.api.crm.dynamics.com/api/data/v9.0/$metadata#contacts(fullname,emailaddress1,telephone1)",
  "value": [
    {
      "@odata.etag": "W/\"1162014\"",
      "fullname": "Cat Francis",
      "emailaddress1": "Cat@cohowinery.com",
      "telephone1": "123-879-9879",
      "contactid": "51a0e5b9-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1162210\"",
      "fullname": "Tomasz Bochenek",
      "emailaddress1": "tom@cohowinery.com",
      "telephone1": "456-698-4581",
      "contactid": "1fa1e5b9-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1162593\"",
      "fullname": "Kari Furse",
      "emailaddress1": "kari@cohowinery.com",
      "telephone1": "178-854-4576",
      "contactid": "9ba2e5b9-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1162714\"",
      "fullname": "Wilson Pais",
      "emailaddress1": "wilson@cohowinery.com",
      "telephone1": "456-698-4582",
      "contactid": "6fa5e5b9-88df-e311-b8e5-6c3be5a8b200"
    }
  ]
}