CRM16 - 从 WebApi 触发自定义操作
CRM16 - Trigger custom action from WebApi
我在 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 和一条消息:
Request message has unresolved parameters.
我可以对同一站点进行其他调用并获取所有联系人作为示例
这是什么意思,我该如何解决?
What does this mean and how do i fix it ?
引用 Request message has unresolved parameters.
In CRM when you get this error while calling action. then there may be
three reasons behind that
- some parameters you are passing wrong. (make sure action name is correctly pass)
- your action is not activated
- your action name is duplicate and one action is in active mode and other is in draft.(as this is done from CRM side that one has to be in
draft only two same name action wont be active at same time.)
没有。 2 已经得到处理,因为它已经声明自定义操作已激活。
没有。 3 在链接的文章中得到解决,如果您可能已在 CRM 中导入操作两次或无意中创建了两个具有相同名称的操作,则该操作是合理的。
对于第一个地址,我建议创建一个对象模型来保存要发送的数据
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
When invoking a bound function, you must include the full name of the
function including the Microsoft.Dynamics.CRM
namespace. If you do not
include the full name, you will get the following error: Status
Code:400 Request message has unresolved parameters.
把名字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
中的欺骗
我前段时间解决了这个问题,但没有时间回来回答它。就我而言,问题是我自己提出请求的方式
而不是问题中说明的以下方式:
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
我在 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 和一条消息:
Request message has unresolved parameters.
我可以对同一站点进行其他调用并获取所有联系人作为示例
这是什么意思,我该如何解决?
What does this mean and how do i fix it ?
引用 Request message has unresolved parameters.
In CRM when you get this error while calling action. then there may be three reasons behind that
- some parameters you are passing wrong. (make sure action name is correctly pass)
- your action is not activated
- your action name is duplicate and one action is in active mode and other is in draft.(as this is done from CRM side that one has to be in draft only two same name action wont be active at same time.)
没有。 2 已经得到处理,因为它已经声明自定义操作已激活。
没有。 3 在链接的文章中得到解决,如果您可能已在 CRM 中导入操作两次或无意中创建了两个具有相同名称的操作,则该操作是合理的。
对于第一个地址,我建议创建一个对象模型来保存要发送的数据
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
When invoking a bound function, you must include the full name of the function including the
Microsoft.Dynamics.CRM
namespace. If you do not include the full name, you will get the following error: Status Code:400 Request message has unresolved parameters.
把名字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
我前段时间解决了这个问题,但没有时间回来回答它。就我而言,问题是我自己提出请求的方式
而不是问题中说明的以下方式:
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