当前位置:  开发笔记 > 编程语言 > 正文

CRM16 - 从WebApi触发自定义操作

如何解决《CRM16-从WebApi触发自定义操作》经验,为你挑选了1个好方法。

我在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,其中包含一条消息:

请求消息具有未解析的参数.

我可以对同一个站点进行其他调用,并以所有联系人为例

这是什么意思,我该如何解决?



1> Nkosi..:

这是什么意思,我该如何解决?

引用请求消息具有未解析的参数.

在CRM中,当您在调用操作时收到此错误.那背后可能有三个原因

    你传递错误的一些参数.(确保动作名称正确传递)

    您的操作未激活

    你的动作名称是重复的,一个动作处于活动模式,另一个动作处于草稿状态.(因为这是从CRM方面完成的,一个人必须在草稿中,只有两个同名动作同时不活动.)

2号已经处理好,因为它已经声明自定义动作已被激活.

如果您在CRM中导入了两次操作或无意中创建了两个具有相同名称的操作,则链接文章中会解决第3项问题.

为了解决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:使用Web API操作

调用绑定函数时,必须包含函数的全名,包括Microsoft.Dynamics.CRM命名空间.如果您不包含全名,您将收到以下错误:状态代码:400请求消息具有未解析的参数.

推荐阅读
个性2402852463
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有