【问题标题】:How can I read result of web api call from Dynamics 365?如何从 Dynamics 365 读取 Web api 调用的结果?
【发布时间】:2021-09-20 17:29:50
【问题描述】:

我尝试从 Dynamics 365 Sales 中检索记录。我在 Azure 中创建了一个应用注册,我可以根据这个应用获取令牌。

另外,我可以调用 HTTP 客户端。但我不知道如何读取 HTTP 调用的结果。

Microsoft 仅发布了 WhoAmIRequest 示例,但我找不到其他实体的示例。

这是我的示例代码。我尝试读取 body 对象。

try
{
    string serviceUrl = "https://****.crm4.dynamics.com/";
    string clientId = "******";
    string clientSecret = "*******";
    string tenantId = "*******";

    A***.Library.Utility.MSCRM mscrm = new Library.Utility.MSCRM(serviceUrl, clientId, clientSecret, tenantId);
    var token = await mscrm.GetTokenAsync();

    Console.WriteLine(token);

    using (HttpClient client = new HttpClient())
    {
        client.BaseAddress = new Uri(serviceUrl);
        client.Timeout = new TimeSpan(0, 2, 0);  //2 minutes  
        client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
        client.DefaultRequestHeaders.Add("OData-Version", "4.0");
        client.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/json"));

        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "/api/data/v9.0/accounts");

        // Set the access token
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

        HttpResponseMessage response = client.SendAsync(request).Result;

        if (response.IsSuccessStatusCode)
        {
            // Get the response content and parse it.  
            var responseStr = response.Content.ReadAsStringAsync();

            JObject body = JObject.Parse(response.Content.ReadAsStringAsync().Result);
        }
    }
}
catch(Exception e)
{
    Console.WriteLine(e.Message);
}

这是body对象的结果。

【问题讨论】:

    标签: microsoft-dynamics dynamics-crm-webapi common-data-service dataverse dynamics-365-sales


    【解决方案1】:

    您可以使用这些语法中的任何一种来读取值。 Read more

    JObject body = JObject.Parse(response.Content.ReadAsStringAsync().Result);
    
    // Can use either indexer or GetValue method (or a mix of two)
    body.GetValue("obs_detailerconfigid");
    body["obs_detailerconfigid"];
    

    【讨论】:

    • 谢谢,阿伦。它与检索工作。但是当我尝试检索多个时,我需要先获取“值”标签,然后才能获取值。这对我来说似乎有点复杂。你有比这更好的方法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 2017-05-06
    • 2022-12-07
    相关资源
    最近更新 更多