Search code examples
vb.netasp.net-web-apidynamics-crmcrmdynamics-crm-webapi

CRM16 - Trigger custom action from WebApi


I've built a custom action in CRM that I need to fire through its WebAPI. The custom action is activated and I got no errors in CRM while creating it.

enter image description here

I try to call this action from a VB.NET application like:

Dim httpch As New HttpClientHandler
Dim requestUri As String = "contacts(1fcfd54a-15d3-e611-80dc-0050569ea396)/Microsoft.Dynamics.CRM.new_addnotetocontact"
httpch.Credentials = New NetworkCredential("username", "password", "domain")
Dim httpClient As New HttpClient(httpch)
httpClient.BaseAddress = New Uri(CRMWebApiUri)
httpClient.Timeout = New TimeSpan(0, 2, 0)
httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0")
httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0")
httpClient.DefaultRequestHeaders.Add("Prefer", "odata.include-annotations='OData.Community.Display.V1.FormattedValue'")
httpClient.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
Dim jsonNote As JObject = New JObject(New JProperty("NoteTitle", "'Mails have been deleted'"), New JProperty("NoteText", "This contacts SmarterMail data has been deleted due to inactivity"))
Dim postData = New StringContent(jsonNote.ToString(), Encoding.UTF8, "application/json")

Dim retrieveContactResponse As HttpResponseMessage = httpClient.PostAsync(requestUri, postData).Result

What i get back is a status 400 with a message:

Request message has unresolved parameters.

I can make other calls to the same site and get all contacts as an example

What does this mean and how do I fix it ?


Solution

  • I fixed this some time ago but haven't got the time to get back to answer it. In my case what was the issue was the way i made the request it self

    Instead of the following way, which is stated in the question:

    Dim postData = New StringContent(jsonNote.ToString(), Encoding.UTF8, "application/json")
    Dim retrieveContactResponse As HttpResponseMessage = httpClient.PostAsync(requestUri, postData).Result
    

    Instead of using the httpClient.PostAsync method and providing the StringContent object directly, I used the HttpRequestMessage object and giving that the StringContent object, and then provide the HttpRequestMessage object to the SendAsync method of the httpClient, and that seems to have solved my issue, as it is working now. Also notice that in the original question i had quotation marks in the value of the first JProperty in the JObject to be posted, I'm not sure this has anything to do with it though, but just posting it here as it is different from original code:

    Dim jsonNote As JObject = New JObject(New JProperty("NoteTitle", "Mails have been deleted"), New JProperty("NoteText", "This contact's SmarterMail data has been deleted automatically due to inactivity on their CRM account"))
    ...
    Dim reqMsg As New HttpRequestMessage(HttpMethod.Post, CRMWebApiUri + requestUri)
    reqMsg.Content = New StringContent(jsonNote.ToString(), Encoding.UTF8, "application/json")
    Dim retrieveContactResponse As HttpResponseMessage = httpClient.SendAsync(reqMsg).Result