简体   繁体   中英

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:

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 APP | WebApi | OnPremise CRM

So, when I call the WebApi, there is a controller that turns my request into a OrganizationRequest:

Request for 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

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.

Another option could be sending the request through web and stop using the IOrganizationService but I cannot change that.

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.

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. 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. Ignore the XRM.Webapi command but interesting for you would be passing parameters in 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. You can definitely call Action from Webapi of Dynamics. I just used Postman to call Action and I was successful. Blog reference to use Postman for CRM webapi

Below Body as json in Postman and I get Action to run.

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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