VB.NET UWP/WinRT 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)"
}'

VB.NET UWP/WinRT Example

Dim rest As New Chilkat.Rest
Dim success As Boolean

'  URL: https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/contacts
Dim bTls As Boolean = True
Dim port As Integer = 443
Dim bAutoReconnect As Boolean = True
success = Await rest.ConnectAsync("my-dynamics-domain.api.crm.dynamics.com",port,bTls,bAutoReconnect)
If (success <> True) Then
    Debug.WriteLine("ConnectFailReason: " & rest.ConnectFailReason)
    Debug.WriteLine(rest.LastErrorText)
    Exit Sub
End If


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

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

Dim sbRequestBody As New Chilkat.StringBuilder
json.EmitSb(sbRequestBody)
Dim sbResponseBody As New Chilkat.StringBuilder
success = Await rest.FullRequestSbAsync("POST","/api/data/v9.0/contacts",sbRequestBody,sbResponseBody)
If (success <> True) Then
    Debug.WriteLine(rest.LastErrorText)
    Exit Sub
End If

Dim respStatusCode As Integer = rest.ResponseStatusCode
If (respStatusCode >= 400) Then
    Debug.WriteLine("Response Status Code = " & respStatusCode)
    Debug.WriteLine("Response Header:")
    Debug.WriteLine(rest.ResponseHeader)
    Debug.WriteLine("Response Body:")
    Debug.WriteLine(sbResponseBody.GetAsString())
    Exit Sub
End If