Unicode C++ 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"

Unicode C++ Example

#include <CkRestW.h>
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    CkRestW rest;
    bool success;

    //  URL: https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/contacts
    bool bTls = true;
    int port = 443;
    bool bAutoReconnect = true;
    success = rest.Connect(L"my-dynamics-domain.api.crm.dynamics.com",port,bTls,bAutoReconnect);
    if (success != true) {
        wprintf(L"ConnectFailReason: %d\n",rest.get_ConnectFailReason());
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

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

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

    CkStringBuilderW sbResponseBody;
    success = rest.FullRequestNoBodySb(L"GET",L"/api/data/v9.0/contacts",sbResponseBody);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    int respStatusCode = rest.get_ResponseStatusCode();
    if (respStatusCode >= 400) {
        wprintf(L"Response Status Code = %d\n",respStatusCode);
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",rest.responseHeader());
        wprintf(L"Response Body:\n");
        wprintf(L"%s\n",sbResponseBody.getAsString());
        return;
    }

    CkJsonObjectW jsonResponse;
    jsonResponse.LoadSb(sbResponseBody);

    int i;
    int count_i;

    const wchar_t *odataContext = jsonResponse.stringOf(L"\"@odata.context\"");
    i = 0;
    count_i = jsonResponse.SizeOfArray(L"value");
    while (i < count_i) {
        jsonResponse.put_I(i);
        const wchar_t *odataEtag = jsonResponse.stringOf(L"value[i].\"@odata.etag\"");
        const wchar_t *fullname = jsonResponse.stringOf(L"value[i].fullname");
        const wchar_t *emailaddress1 = jsonResponse.stringOf(L"value[i].emailaddress1");
        const wchar_t *telephone1 = jsonResponse.stringOf(L"value[i].telephone1");
        const wchar_t *contactid = jsonResponse.stringOf(L"value[i].contactid");
        i = i + 1;
    }
    }

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"
    }
  ]
}