【问题标题】:Is it possible to call an Dynamics CRM action with a JSON request through a WebApi with IOrganizationService?是否可以通过带有 IOrganizationService 的 WebApi 调用带有 JSON 请求的 Dynamics CRM 操作?
【发布时间】:2020-10-06 19:21:58
【问题描述】:

TL;DR:

我正在调用 WebApi,WebApi 针对 CRM 进行身份验证并使用 IOrganizationService,所以我的请求是 JObject 而不是 Entity 或 EntityReference,它给了我这个错误:

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.

上下文:

我用 Angular 构建了一个 Web 应用程序,并构建了一个 WebApi,因此我可以在 CRM 中调用一些自定义操作:

Angular 应用程序 | WebAPI |本地 CRM

所以,当我调用 WebApi 时,有一个控制器将我的请求转换为 OrganizationRequest:

WebApi 请求:

{
    "ActionName": "custom_actionname",
    "Parameters": 
    [
        {
            "Key": "EntityInputParameter1", 
            "Value": {"@odata.type":"Microsoft.Dynamics.CRM.any_entity"}
        }
    ]
}

我在我的 WebApi 上阅读了此请求并将其转换为对 CRM 的请求

申请 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);

当我提出请求时,它给了我以下错误:

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.

如果我直接向它工作的操作发出请求,但由于安全策略,我不能这样做。

一种选择是将请求转换为有效的 CRM 请求(将 {"@odata.type":"Microsoft.Dynamics.CRM.any_entity} 解析为 Entity 类型),但 CRM 有很多解析场景并且可能非常复杂。

另一种选择是通过网络发送请求并停止使用IOrganizationService,但我无法更改。

我提出这个问题是为了让任何有这个错误的人都能找到“解决方案”,因为我搜索了很多,没有人直接提到这种行为。

我可能正在将我的 InputEntityParameter 转换为字符串,并且我将发送 JSON,因此我可以在我的操作中解析 JSON,但我正在查看是否有其他人有此错误或其他方法。

【问题讨论】:

  • 你的EntityInputParameter1是什么类型我怀疑是实体引用类型?
  • @AnkUser 类型为实体

标签: c# asp.net-web-api dynamics-crm


【解决方案1】:

我在我的一个开发环境中使用实体作为参数对其进行了测试。

下面是我在控制台应用程序中使用实体作为参数触发动作的代码。运行成功

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);

以下是使用 CRM Webapi 执行带参数操作的 Javascript 代码。忽略 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);
    }
);

我可以确认您正在混合使用 Webapi 和 orgservice 调用。您绝对可以从 Dynamics 的 Webapi 调用 Action。我刚刚使用 Postman 调用 Action 并且成功了。 Blog reference to use Postman for CRM webapi

在 Postman 中作为 json 的 Body 下方,我可以运行 Action。

{
    "Param1":"string test",
    "Param2":{
        "accountid":"b6b35fd0-b9c3-e311-88e2-00505693000c",
        "@odata.type":"Microsoft.Dynamics.CRM.account"
            }
}

【讨论】:

  • 我真正想要的是从客户端调用web api(自定义web api)然后自定义web api调用service.Execute,所以web api的输入参数是"Param1": { "typeName": "mscrm.account","structuralProperty": 5}然后,web api 使用request.Parameters.Add("Param1", { "typeName": "mscrm.account","structuralProperty": 5}))); 调用 crm 操作
  • 我可能错了,但我认为您将 WebAPI 与 CRM 的 OrganizationService 混合在一起。 Orgservice 与 CRM 的 SDK 一起使用,并在使用 crm 的服务和 Webapi 时被利用,因为您知道它是一个 http post call。
  • 我可以确认您正在混合使用 Webapi 和 orgservice 调用。您绝对可以从 Dynamics 的 Webapi 调用 Action。我刚刚使用 Postman 调用 Action 并且成功了。我已经更新了我的答案。
  • 是的,请参阅博客。也请您投票并接受答案。
  • 不,你没有抓住重点。您可以使用客户端 ID 和密码直接访问 crm 的 webapi。你不需要这个中间件。您应该使用 google hot 来使用 webapi 和 postman 进行动态 365。您将了解如何在活动目录中创建应用程序、如何利用 webapi 功能等所有步骤。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-06
  • 2019-09-29
相关资源
最近更新 更多