[英]Is it possible to call an Dynamics CRM action with a JSON request through a WebApi with IOrganizationService?
TL;DR:特尔;博士:
I am calling a WebApi, the WebApi authenticates against the CRM and use the IOrganizationService, so my request is a JObject and not an Entity or EntityReference, it gives me this error:我正在调用 WebApi,WebApi 针对 CRM 进行身份验证并使用 IOrganizationService,所以我的请求是 JObject 而不是实体或实体引用,它给了我这个错误:
Error: Type 'Newtonsoft.Json.Linq.JToken' is a recursive collection data contract which is not supported. Consider modifying the definition of collection 'Newtonsoft.Json.Linq.JToken' to remove references to itself.
Context:语境:
I built a web application in angular and I built a WebApi so I can call some custom actions in CRM:我用 angular 构建了一个 Web 应用程序,并构建了一个 WebApi,以便我可以在 CRM 中调用一些自定义操作:
Angular APP |角APP | WebApi |网络API | OnPremise CRM内部客户关系管理
So, when I call the WebApi, there is a controller that turns my request into a OrganizationRequest:因此,当我调用 WebApi 时,有一个控制器将我的请求转换为 OrganizationRequest:
Request for WebApi:请求 WebApi:
{
"ActionName": "custom_actionname",
"Parameters":
[
{
"Key": "EntityInputParameter1",
"Value": {"@odata.type":"Microsoft.Dynamics.CRM.any_entity"}
}
]
}
I read this request on my WebApi and turn that into a request for CRM我在我的 WebApi 上阅读了此请求并将其转换为对 CRM 的请求
Request for CRM:客户关系管理请求:
OrganizationRequest request = new OrganizationRequest("custom_actionname");
request.Parameters["EntityInputParameter1"] = {"@odata.type":"Microsoft.Dynamics.CRM.any_entity"} // This is a JObject
OrganizationResponse response = service.Execute(request);
When I make the request, it gives me the following error:当我提出请求时,它给了我以下错误:
Error: Type 'Newtonsoft.Json.Linq.JToken' is a recursive collection data contract which is not supported. Consider modifying the definition of collection 'Newtonsoft.Json.Linq.JToken' to remove references to itself.
If I make the request directly to the action it works, but I cannot do that due security policies.如果我直接向其工作的操作发出请求,但由于安全策略,我不能这样做。
One option could be turn the request into a valid CRM request (parsing {"@odata.type":"Microsoft.Dynamics.CRM.any_entity}
into a Entity
type) but CRM has a lot of parsing escenarios and could be very complex.一种选择是将请求转换为有效的 CRM 请求(将{"@odata.type":"Microsoft.Dynamics.CRM.any_entity}
解析为Entity
类型),但 CRM 有很多解析场景并且可能非常复杂。
Another option could be sending the request through web and stop using the IOrganizationService
but I cannot change that.另一种选择可能是通过 Web 发送请求并停止使用IOrganizationService
但我无法更改。
I am making this question so anybody that has this error can find the "solution" because I searched a lot and nobody refers this behavior directly.我正在提出这个问题,因此任何有此错误的人都可以找到“解决方案”,因为我搜索了很多,但没有人直接提及此行为。
I am probably turning my InputEntityParameter into string, and I will send the JSON, so I can parse the JSON on my action, but I was looking if anybody else had this error or another approach.我可能正在将我的 InputEntityParameter 转换为字符串,然后我将发送 JSON,这样我就可以在我的操作中解析 JSON,但我正在寻找其他人是否有此错误或其他方法。
I tested it on one of my Dev Environment with Entity as Parameter.我在我的一个开发环境中使用实体作为参数对其进行了测试。
Below is the code I used in console application to fire Action with Entity as parameter.下面是我在控制台应用程序中使用的代码,以 Entity 作为参数触发 Action。 It ran successfully它运行成功
var request = new OrganizationRequest("new_test");
//request.Parameters.Add("Target", xAccountReference);
request.Parameters.Add("Param2", "abc");
request.Parameters.Add("Param1", new Entity("account",Guid.Parse("2fe32f22-d01d-ea11-80fa-005056936c69")));
Service.Execute(request);
Below is the Javascript code which used CRM Webapi to execute Action with Parameter.以下是使用 CRM Webapi 执行带参数操作的 Javascript 代码。 Ignore the XRM.Webapi command but interesting for you would be passing parameters in webapi.忽略 XRM.Webapi 命令,但您感兴趣的是在 webapi 中传递参数。
var parameters = {};
parameters.Param2 = "abcd";
var param1 = {};
param1.accountid = "2fe32f22-d01d-ea11-80fa-005056936c69"; //Delete if creating new record
param1["@odata.type"] = "Microsoft.Dynamics.CRM.account";
parameters.Param1 = param1;
var new_testRequest = {
Param2: parameters.Param2,
Param1: parameters.Param1,
getMetadata: function() {
return {
boundParameter: null,
parameterTypes: {
"Param2": {
"typeName": "Edm.String",
"structuralProperty": 1
},
"Param1": {
"typeName": "mscrm.account",
"structuralProperty": 5
}
},
operationType: 0,
operationName: "new_test"
};
}
};
Xrm.WebApi.online.execute(new_testRequest).then(
function success(result) {
if (result.ok) {
//Success - No Return Data - Do Something
}
},
function(error) {
Xrm.Utility.alertDialog(error.message);
}
);
I can confirm that you are mixing Webapi and orgservice call.我可以确认您正在混合 Webapi 和 orgservice 调用。 You can definitely call Action from Webapi of Dynamics.你绝对可以从 Dynamics 的 Webapi 调用 Action。 I just used Postman to call Action and I was successful.我刚用 Postman 调用 Action 就成功了。 Blog reference to use Postman for CRM webapi 使用 Postman for CRM webapi 的博客参考
Below Body as json in Postman and I get Action to run.在 Postman 中作为 json 的 Body 下面,我可以运行 Action。
{
"Param1":"string test",
"Param2":{
"accountid":"b6b35fd0-b9c3-e311-88e2-00505693000c",
"@odata.type":"Microsoft.Dynamics.CRM.account"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.