我在CRM中构建了一个自定义操作,我需要通过其WebAPI来解决这个问题。自定义操作已激活,在创建CRM时我没有错误。
我尝试从VB.NET应用程序调用此操作,如:
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
我得到的是状态400并带有消息:
请求消息包含未解析的参数。
我可以对同一网站进行其他调用并以所有联系人为例
这是什么意思,我该如何解决?
答案 0 :(得分:6)
这是什么意思,我该如何解决?
引用Request message has unresolved parameters.
在CRM中,当您在调用操作时出现此错误。然后可能会有
背后的三个原因
- 你传递错误的一些参数。 (确保动作名称正确通过)
- 您的操作未激活
- 您的操作名称是重复的,一个操作处于活动模式,另一个处于草稿中(因为这是从CRM方面完成的,必须在 草稿中只有两个同名动作不会同时处于活动状态。)
醇>
没有。 2已经处理,因为它已经声明自定义动作被激活。
没有。如果您在CRM中导入了两次操作或无意中创建了两个具有相同名称的操作,则链接文章中会解决此问题,并且这是合理的。
要解决1号问题,我建议创建一个对象模型来保存要发送的数据
Public Class Note
Public Property NoteTitle As String
Public Property NoteText As String
End Class
CRM对于正确的参数格式化非常挑剔。参数名称也区分大小写。 ''
中的NoteTitle
会在序列化时出现问题。
此外,如果可能的话,使用 NewtonSoft.Json 来制作JSON有效负载,而不是尝试自己构建它。
'Handler with credentials
Dim httpClientHandler As New HttpClientHandler With {
.Credentials = New NetworkCredential("username", "password", "domain")}
'Create and configure HTTP Client
Dim httpClient As New HttpClient(httpClientHandler) With {
.BaseAddress = New Uri(CRMWebApiUri),
.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"))
'Create and populate data to be sent
Dim model As New Note With {
.NoteTitle = "Mails have been deleted",
.NoteText = "This contacts SmarterMail data has been deleted due to inactivity"}
'Serialize mode to well formed JSON
Dim json As String = JsonConvert.SerializeObject(model)
Dim postData = New StringContent(json, Encoding.UTF8, "application/json")
'invoking action using the fully qualified namespace of action message
Dim requestUri As String = "contacts(1fcfd54a-15d3-e611-80dc-0050569ea396)/Microsoft.Dynamics.CRM.new_addnotetocontact"
'POST the data
Dim retrieveContactResponse As HttpResponseMessage = Await httpClient.PostAsync(requestUri, postData)
其他参考Dynamics CRM 2016: Use Web API actions
调用绑定函数时,必须包含该函数的全名 函数包括
Microsoft.Dynamics.CRM
命名空间。如果你不 包含全名,您将收到以下错误:状态 代码:400请求消息包含未解析的参数。
答案 1 :(得分:0)
将名称new_addnotetocontact
更改为new_AddNoteToContact
,它会起作用。
Dim requestUri As String = "contacts(1fcfd54a-15d3-e611-80dc-0050569ea396)/Microsoft.Dynamics.CRM.new_AddNoteToContact"
流程架构如下所示:
<Action Name="new_AddNoteToContact" IsBound="true">
<Parameter Name="entity" Type="mscrm.contact" Nullable="false" />
<Parameter Name="NoteTitle" Type="Edm.String" Nullable="false" Unicode="false" />
<Parameter Name="NoteText" Type="Edm.String" Nullable="false" Unicode="false" />
<ReturnType Type="mscrm.annotation" Nullable="false" />
</Action>
唯一名称: new_AddNoteToContact
参考:https://msdn.microsoft.com/en-us/library/mt607600.aspx#Anchor_3
更新:如果您在创建操作后编辑了唯一名称,则会在Processes
实体中创建重复项。请从Adv.Find中删除欺骗
答案 2 :(得分:0)
我前段时间修好了这件事,但还没有时间回过头来回答它。在我的情况下,问题是我自己提出请求的方式
而不是以下方式,在问题中说明:
Dim postData = New StringContent(jsonNote.ToString(), Encoding.UTF8, "application/json")
Dim retrieveContactResponse As HttpResponseMessage = httpClient.PostAsync(requestUri, postData).Result
我没有使用httpClient.PostAsync方法并直接提供StringContent对象,而是使用了HttpRequestMessage对象并给出了StringContent对象,然后将HttpRequestMessage对象提供给httpClient的SendAsync方法,这似乎已经解决了我的问题,因为它现在正在运作。另请注意,在原始问题中,我在JObject中的第一个JProperty的值中有引号,但我不确定这与它有什么关系,但只是在这里发布,因为它是不同的来自原始代码:
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