Swift Dynamics CRM: Lookup Contact ID using Full Name

Back to Index

Find a contactid given a fullname.

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=contactid" \
  -d "$filter=fullname eq 'Tomasz Bochenek'" \
  -H "Authorization: Bearer DYNAMICS_CRM_ACCESS_TOKEN"

Swift Example


func chilkatTest() {
    let rest = CkoRest()
    var success: Bool

    //  URL: https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/contacts
    var bTls: Bool = true
    var port: Int = 443
    var bAutoReconnect: Bool = true
    success = rest.Connect("my-dynamics-domain.api.crm.dynamics.com", port: port, tls: bTls, autoReconnect: bAutoReconnect)
    if success != true {
        print("ConnectFailReason: \(rest.ConnectFailReason.intValue)")
        print("\(rest.LastErrorText)")
        return
    }

    rest.AddQueryParam("$select", value: "contactid")
    rest.AddQueryParam("$filter", value: "fullname eq 'Tomasz Bochenek'")

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

    let sbResponseBody = CkoStringBuilder()
    success = rest.FullRequestNoBodySb("GET", uriPath: "/api/data/v9.0/contacts", sb: sbResponseBody)
    if success != true {
        print("\(rest.LastErrorText)")
        return
    }

    var respStatusCode: Int = rest.ResponseStatusCode.intValue
    if respStatusCode >= 400 {
        print("Response Status Code = \(respStatusCode)")
        print("Response Header:")
        print("\(rest.ResponseHeader)")
        print("Response Body:")
        print("\(sbResponseBody.GetAsString())")
        return
    }

    let jsonResponse = CkoJsonObject()
    jsonResponse.LoadSb(sbResponseBody)

    var i: Int
    var count_i: Int

    var odataContext: String? = jsonResponse.StringOf("\"@odata.context\"")
    i = 0
    count_i = jsonResponse.SizeOfArray("value").intValue
    while i < count_i {
        jsonResponse.I = i
        var odataEtag: String? = jsonResponse.StringOf("value[i].\"@odata.etag\"")
        var contactid: String? = jsonResponse.StringOf("value[i].contactid")
        i = i + 1
    }


}

Sample JSON Response Body

{
  "@odata.context": "https://mydomain.api.crm.dynamics.com/api/data/v9.0/$metadata#contacts(contactid)",
  "value": [
    {
      "@odata.etag": "W/\"1162210\"",
      "contactid": "1fa1e5b9-88df-e311-b8e5-6c3be5a8b200"
    }
  ]
}