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

C# 是否可以通过带有IOrOrganizationService的WebApi通过JSON请求调用Dynamics CRM操作?,c#,asp.net-web-api,dynamics-crm,C#,Asp.net Web Api,Dynamics Crm,TL;医生: { "ActionName": "custom_actionname", "Parameters": [ { "Key": "EntityInputParameter1", "Value": {"@odata.type":"Microsoft.

TL;医生:

{
    "ActionName": "custom_actionname",
    "Parameters": 
    [
        {
            "Key": "EntityInputParameter1", 
            "Value": {"@odata.type":"Microsoft.Dynamics.CRM.any_entity"}
        }
    ]
}
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);
我正在调用一个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.
上下文:

{
    "ActionName": "custom_actionname",
    "Parameters": 
    [
        {
            "Key": "EntityInputParameter1", 
            "Value": {"@odata.type":"Microsoft.Dynamics.CRM.any_entity"}
        }
    ]
}
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);
我在angular中构建了一个web应用程序,并构建了一个WebApi,以便在CRM中调用一些自定义操作:

Angular应用程序| WebApi |内部客户关系管理

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

请求WebApi:

{
    "ActionName": "custom_actionname",
    "Parameters": 
    [
        {
            "Key": "EntityInputParameter1", 
            "Value": {"@odata.type":"Microsoft.Dynamics.CRM.any_entity"}
        }
    ]
}
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);
我在我的WebApi上阅读了这个请求,并将其转化为CRM请求

客户关系管理请求:

{
    "ActionName": "custom_actionname",
    "Parameters": 
    [
        {
            "Key": "EntityInputParameter1", 
            "Value": {"@odata.type":"Microsoft.Dynamics.CRM.any_entity"}
        }
    ]
}
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,但我正在查看是否有其他人有此错误或其他方法。

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

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

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调用操作。我刚刚使用Postman调用操作,我成功了

在Postman中以json的形式出现在Body下面,我将运行操作

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

您的
EntityInputParameter1是什么类型的?
我怀疑它是实体引用类型?@AnkUser type是EntityI我真正想要的是从客户端调用web api(自定义web api),然后自定义web api调用服务。执行,因此web api的输入参数将是
参数“Param1”:{“typeName”:“mscrm.account”,“StructureProperty“:5}
然后,web api使用
request.Parameters.Add(“Param1”,“typeName”:“mscrm.account”,“StructureProperty”:5})调用crm操作))
我可能错了,但我认为您是在将WebAPI与CRM的OrganizationService混合使用。Orgservice与CRM的SDK一起使用,并且在使用CRM的服务时加以利用。对于WebAPI,正如您所知,这是一个http post调用。是的!这就是我所做的,因为我无法直接调用CRM操作,通过http调用,我正在使用http调用.net web apihttp调用,然后使用服务调用Orgservice。执行。我可以确认您混合了Webapi和Orgservice调用。您完全可以从Dynamics的Webapi调用操作。我刚刚使用Postman调用操作,我成功了。我已更新了我的答案。{{webapiurl}是crm组织的web api端点吗?