odatadynamics-crmmicrosoft-dynamicsdynamics-crm-webapimicrosoft-dynamics-webapi

Create annotation to a contact entity in Microsoft Dynamics CRM by API


This question is related to Microsoft Dynamics CRM 2015, that I'm calling through API.

I create contact entity:

POST [organization URI]/api/data/contacts
Content-Type: application/json; charset=utf-8
Accept: application/json
{
    "emailaddress1": "myemail@example.com",
}

It works, I see new record, after I log into the panel. And I can call it through the API:

[organization URI]/api/data/contacts(f76e4e7c-ea61-e511-80fd-3863bb342b00)
{
  "@odata.context":"[organization URI]/api/data/$metadata#contacts/$entity",
  "@odata.etag":"W/\"460199\"",
  ...
  "contactid":"f76e4e7c-ea61-e511-80fd-3863bb342b00",
  "emailaddress1":"myemail@example.com",
  ....
}

Next thing I want to do, is to add annotation record associated with that contact. Following the guide I call:

POST [organization URI]/api/data/annotations
Content-Type: application/json; charset=utf-8
Accept: application/json
{
    "notetext": "TEST",
    'contact@odata.bind': 'contacts(f76e4e7c-ea61-e511-80fd-3863bb342b00)'
}

But it returns 400 error:

An undeclared property 'contact' which only has property annotations in the payload but no property value was found in the payload. In OData, only declared navigation properties and declared named streams can be represented as properties without values.

When I call:

POST [organization URI]/api/data/annotations
Content-Type: application/json; charset=utf-8
Accept: application/json
{
    "notetext": "TEST",
}

New entity is created, but without a relation to contact.

How to properly compose this POST request? What am I missing here? I suspect, that contact@odata.bind should be presented somehow different, I've tried contactid@odata.bind, object@odata.bind, objectid@odata.bind - but no effects.

Any ideas?


Solution

  • I've found this working, but in two requests:

    POST [organization URI]/api/data/annotations
    Content-Type: application/json; charset=utf-8
    Accept: application/json
    {
        "notetext": "TEST"
    }
    
    POST [organization URI]/api/data/contacts(f76e4e7c-ea61-e511-80fd-3863bb342b00)/Contact_Annotation/$ref
    Content-Type: application/json; charset=utf-8
    Accept: application/json
    {
        "@odata.id": "[organization URI]/annotations(annotation_id_from_first_request)"
    }
    

    Edit:

    annotation_id_from_first_request value is taken form first request's response.