Unicode C++ Dynamics CRM: Create an Contact

Back to Index

Creates a new contact for a specific account. In this example, the account is Fourth Coffee, where the accountid = b6a19cdd-88df-e311-b8e5-6c3be5a8b200. Success is indicated by a 204 response status code with an empty response body.

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

CURL Command

curl -X POST https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/contacts \
  -H "Accept: application/json" \
  -H "Content-Type: application/json; charset=utf-8" \
  -H "OData-MaxVersion: 4.0"  \
  -H "OData-Version: 4.0" \
  -H "Authorization: Bearer DYNAMICS_CRM_ACCESS_TOKEN" \
  -d '{
    "fullname": "Gabby Cannata",
    "firstname": "Gabby",
    "lastname": "Cannata",
    "emailaddress1": "gabby@fourthcoffee.com",
    "address1_line1": "Carrera 1c No 10-01",
    "address1_city": "Bogota",
    "address1_country": "Colombia",
    "telephone1": "408-233-1939",
    "jobtitle": "Purchasing Assistant",
    "parentcustomerid_account@odata.bind": "/accounts(b6a19cdd-88df-e311-b8e5-6c3be5a8b200)"
}'

Unicode C++ Example

#include <CkRestW.h>
#include <CkJsonObjectW.h>
#include <CkStringBuilderW.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;
    }

    CkJsonObjectW json;
    json.UpdateString(L"fullname",L"Gabby Cannata");
    json.UpdateString(L"firstname",L"Gabby");
    json.UpdateString(L"lastname",L"Cannata");
    json.UpdateString(L"emailaddress1",L"gabby@fourthcoffee.com");
    json.UpdateString(L"address1_line1",L"Carrera 1c No 10-01");
    json.UpdateString(L"address1_city",L"Bogota");
    json.UpdateString(L"address1_country",L"Colombia");
    json.UpdateString(L"telephone1",L"408-233-1939");
    json.UpdateString(L"jobtitle",L"Purchasing Assistant");
    json.UpdateString(L"\"parentcustomerid_account@odata.bind\"",L"/accounts(b6a19cdd-88df-e311-b8e5-6c3be5a8b200)");

    rest.AddHeader(L"Content-Type",L"application/json; charset=utf-8");
    rest.AddHeader(L"OData-Version",L"4.0");
    rest.AddHeader(L"Accept",L"application/json");
    rest.AddHeader(L"OData-MaxVersion",L"4.0");
    rest.AddHeader(L"Authorization",L"Bearer DYNAMICS_CRM_ACCESS_TOKEN");

    CkStringBuilderW sbRequestBody;
    json.EmitSb(sbRequestBody);
    CkStringBuilderW sbResponseBody;
    success = rest.FullRequestSb(L"POST",L"/api/data/v9.0/contacts",sbRequestBody,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;
    }
    }