我正在寻找一些简单的样板代码,可以用来对Dynamics 365 API进行查询并获取一些JSON。
最好使用WebClient或HttpClient。没有什么花哨。最简单,可重用的示例将得到答案。
答案 0 :(得分:1)
您可以在SDK示例中找到示例代码。 here对此进行了解释。
一些关键点:
1。阅读代码中的注释。非常重要的一个:
/// Before building this application, you must first modify the following configuration
/// information in the app.config file:
/// - All deployments: Provide connection string service URL's for your organization.
/// - CRM (online): Replace the application settings with the correct values for your
/// Azure app registration.
2。方法ConnectToCRM
将进行身份验证并进行HttpClient
调用
3。代码样本中几乎解释了包括fetchxml在内的每一种查询类型
如果您需要帮助以从Azure注册的CRM appId获取 AccessToken ,请参阅Jason Lattimer blog。
总体简单的样板代码和步骤可以在Inogic blog中找到。
HttpClient httpClient= null;
httpClient = new HttpClient();
//Default Request Headers needed to be added in the HttpClient Object
httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
//Set the Authorization header with the Access Token received specifying the Credentials
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _result.AccessToken);