簡體   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