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)"
}'

C Example

#include <C_CkRest.h>
#include <C_CkJsonObject.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    HCkRest rest;
    BOOL success;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkJsonObject json;
    HCkStringBuilder sbRequestBody;
    HCkStringBuilder sbResponseBody;
    int respStatusCode;

    rest = CkRest_Create();

    //  URL: https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/contacts
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    success = CkRest_Connect(rest,"my-dynamics-domain.api.crm.dynamics.com",port,bTls,bAutoReconnect);
    if (success != TRUE) {
        printf("ConnectFailReason: %d\n",CkRest_getConnectFailReason(rest));
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        return;
    }

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

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

    sbRequestBody = CkStringBuilder_Create();
    CkJsonObject_EmitSb(json,sbRequestBody);
    sbResponseBody = CkStringBuilder_Create();
    success = CkRest_FullRequestSb(rest,"POST","/api/data/v9.0/contacts",sbRequestBody,sbResponseBody);
    if (success != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkJsonObject_Dispose(json);
        CkStringBuilder_Dispose(sbRequestBody);
        CkStringBuilder_Dispose(sbResponseBody);
        return;
    }

    respStatusCode = CkRest_getResponseStatusCode(rest);
    if (respStatusCode >= 400) {
        printf("Response Status Code = %d\n",respStatusCode);
        printf("Response Header:\n");
        printf("%s\n",CkRest_responseHeader(rest));
        printf("Response Body:\n");
        printf("%s\n",CkStringBuilder_getAsString(sbResponseBody));
        CkRest_Dispose(rest);
        CkJsonObject_Dispose(json);
        CkStringBuilder_Dispose(sbRequestBody);
        CkStringBuilder_Dispose(sbResponseBody);
        return;
    }



    CkRest_Dispose(rest);
    CkJsonObject_Dispose(json);
    CkStringBuilder_Dispose(sbRequestBody);
    CkStringBuilder_Dispose(sbResponseBody);

    }