Unicode C Dynamics CRM: Get Accounts

Back to Index

This example queries the accounts entity set and uses the $select system query option to return the name property for each account.

Documentation: https://msdn.microsoft.com/en-us/library/gg334767.aspx#Basic query example

CURL Command

curl -X GET https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/accounts?$select=name \
  -H "Accept: application/json" \
  -H "OData-MaxVersion: 4.0"  \
  -H "OData-Version: 4.0" \
  -H "Authorization: Bearer DYNAMICS_CRM_ACCESS_TOKEN"

Unicode C Example

#include <C_CkRestW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    HCkRestW rest;
    BOOL success;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkStringBuilderW sbResponseBody;
    int respStatusCode;
    HCkJsonObjectW jsonResponse;
    int i;
    int count_i;
    const wchar_t *odataContext;
    const wchar_t *odataEtag;
    const wchar_t *name;
    const wchar_t *accountid;

    rest = CkRestW_Create();

    //  URL: https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/accounts?$select=name
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    success = CkRestW_Connect(rest,L"my-dynamics-domain.api.crm.dynamics.com",port,bTls,bAutoReconnect);
    if (success != TRUE) {
        wprintf(L"ConnectFailReason: %d\n",CkRestW_getConnectFailReason(rest));
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        return;
    }

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

    sbResponseBody = CkStringBuilderW_Create();
    success = CkRestW_FullRequestNoBodySb(rest,L"GET",L"/api/data/v9.0/accounts?$select=name",sbResponseBody);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(sbResponseBody);
        return;
    }

    respStatusCode = CkRestW_getResponseStatusCode(rest);
    if (respStatusCode >= 400) {
        wprintf(L"Response Status Code = %d\n",respStatusCode);
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkRestW_responseHeader(rest));
        wprintf(L"Response Body:\n");
        wprintf(L"%s\n",CkStringBuilderW_getAsString(sbResponseBody));
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(sbResponseBody);
        return;
    }

    jsonResponse = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jsonResponse,sbResponseBody);

    odataContext = CkJsonObjectW_stringOf(jsonResponse,L"\"@odata.context\"");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"value");
    while (i < count_i) {
        CkJsonObjectW_putI(jsonResponse,i);
        odataEtag = CkJsonObjectW_stringOf(jsonResponse,L"value[i].\"@odata.etag\"");
        name = CkJsonObjectW_stringOf(jsonResponse,L"value[i].name");
        accountid = CkJsonObjectW_stringOf(jsonResponse,L"value[i].accountid");
        i = i + 1;
    }



    CkRestW_Dispose(rest);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jsonResponse);

    }

Sample JSON Response Body

{
  "@odata.context": "https://mydomain.api.crm.dynamics.com/api/data/v8.2/$metadata#accounts(name)",
  "value": [
    {
      "@odata.etag": "W/\"1817213\"",
      "name": "A. Datum",
      "accountid": "475b158c-541c-e511-80d3-3863bb347ba8"
    },
    {
      "@odata.etag": "W/\"1817214\"",
      "name": "Adventure Works",
      "accountid": "a8a19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817215\"",
      "name": "Alpine Ski House",
      "accountid": "aaa19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817216\"",
      "name": "Blue Yonder Airlines",
      "accountid": "aca19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817217\"",
      "name": "City Power & Light",
      "accountid": "aea19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817218\"",
      "name": "Coho Winery",
      "accountid": "b0a19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817219\"",
      "name": "Consolidated Messenger",
      "accountid": "b2a19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817220\"",
      "name": "Fabrikam, Inc.",
      "accountid": "b4a19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817221\"",
      "name": "Fourth Coffee",
      "accountid": "b6a19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817222\"",
      "name": "Graphic Design Institute",
      "accountid": "b8a19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817223\"",
      "name": "Humongous Insurance",
      "accountid": "baa19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817224\"",
      "name": "Litware",
      "accountid": "bca19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817225\"",
      "name": "Lucerne Publishing",
      "accountid": "bea19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817226\"",
      "name": "Margie's Travel",
      "accountid": "c0a19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817227\"",
      "name": "Northwind Traders",
      "accountid": "c2a19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817228\"",
      "name": "Proseware, Inc.",
      "accountid": "c4a19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817229\"",
      "name": "School of Fine Art",
      "accountid": "c6a19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817230\"",
      "name": "Southridge Video",
      "accountid": "c8a19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817231\"",
      "name": "Tailspin Toys",
      "accountid": "caa19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817232\"",
      "name": "The Phone Company",
      "accountid": "cca19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817233\"",
      "name": "Trey Research",
      "accountid": "cea19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817234\"",
      "name": "Wide World Importers",
      "accountid": "d0a19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817235\"",
      "name": "Wingtip Toys",
      "accountid": "d2a19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817236\"",
      "name": "Woodgrove Bank",
      "accountid": "d4a19cdd-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1817237\"",
      "name": "Best o' Sales",
      "accountid": "2123455d-be62-e411-80d6-b4b52f567ec8"
    },
    {
      "@odata.etag": "W/\"1817238\"",
      "name": "Microsoft",
      "accountid": "d3ae1b3a-8bd2-e411-80ef-c4346bac7be8"
    }
  ]
}