繁体   English   中英

是否可以通过带有 IOrganizationService 的 WebApi 使用 JSON 请求调用 Dynamics CRM 操作?

[英]Is it possible to call an Dynamics CRM action with a JSON request through a WebApi with IOrganizationService?

特尔;博士:

我正在调用 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.

语境:

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

角APP | 网络API | 内部客户关系管理

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

请求 WebApi:

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

我在我的 WebApi 上阅读了此请求并将其转换为对 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 有很多解析场景并且可能非常复杂。

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

我正在提出这个问题,因此任何有此错误的人都可以找到“解决方案”,因为我搜索了很多,但没有人直接提及此行为。

我可能正在将我的 InputEntityParameter 转换为字符串,然后我将发送 JSON,这样我就可以在我的操作中解析 JSON,但我正在寻找其他人是否有此错误或其他方法。

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

下面是我在控制台应用程序中使用的代码,以 Entity 作为参数触发 Action。 它运行成功

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 就成功了。 使用 Postman for CRM webapi 的博客参考

在 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM