// in appsettings.json file { /DynamicsCRM/: { /BaseUrl/: https://your-crm-instance.api.crm.dynamics.com/api/data/v9.0//, /ClientId/: /your-client-id/, /ClientSecret/: /your-client-secret/, /TenantId/: /your-tenant-id/ }}// in DynamicsCRMService.cs file using System.Net.Http;using System.Net.Http.Headers;using System.Threading.Tasks;using Microsoft.Extensions.Configuration;using Newtonsoft.Json;public class DynamicsCRMService{ private readonly HttpClient _httpClient; public DynamicsCRMService(IConfiguration configuration, HttpClient httpClient) { _httpClient = httpClient; var dynamicsCRMConfig = configuration.GetSection(/DynamicsCRM/); _httpClient.BaseAddress = new System.Uri(dynamicsCRMConfig[/BaseUrl/]); _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(/application/json/)); } public async Task<string> GetPurchaseOrders() { var response = await _httpClient.GetAsync(/purchaseorders/); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStringAsync(); }}//in program file builder.Services.AddHttpClient<DynamicsCRMService>();builder.Services.AddScoped<DynamicsCRMService>();// DynamicsCRMService controller using Microsoft.AspNetCore.Mvc;using System.Threading.Tasks;[ApiController][Route(/[controller]/)]public class PurchaseOrderController : ControllerBase{ private readonly DynamicsCRMService _dynamicsCRMService; public PurchaseOrderController(DynamicsCRMService dynamicsCRMService) { _dynamicsCRMService = dynamicsCRMService; } [HttpGet] public async Task<IActionResult> GetPurchaseOrders() { var purchaseOrders = await _dynamicsCRMService.GetPurchaseOrders(); return Ok(purchaseOrders); }}
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 290,900 Super User 2024 Season 2
Martin Dráb 229,275 Most Valuable Professional
nmaenpaa 101,156