Skip to content

Commit

Permalink
added routes for getting individial records from Contacts and Account…
Browse files Browse the repository at this point in the history
…s lists of Dynamics 365 CRM
  • Loading branch information
Dmitry Kozlov committed Dec 4, 2024
1 parent ae6890d commit bde7f4f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
40 changes: 35 additions & 5 deletions FunctionApp/Dynamics365/CRM/Accounts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,45 @@ public Accounts(HttpClientProvider httpClientProvider, ILogger<Accounts> logger)
}

[Function("D365-CRM-Accounts")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req)
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "crm/accounts/{id?}")] HttpRequest req, Guid? id)
{
_logger.LogInformation("Dynamics365-CRM-Accounts is requested.");

var client = _httpClientProvider.Create();
var accountsJson = await client.GetStringAsync("accounts");
var accounts = JsonValue.Parse(accountsJson);
try
{
var client = _httpClientProvider.Create();

return new OkObjectResult(accounts?["value"]);
if (!id.HasValue)
{
var accountsJson = await client.GetStringAsync("accounts?$select=accountid,name");
var accounts = JsonValue.Parse(accountsJson);
return new OkObjectResult(accounts?["value"]);
}

var accountResponse = await client.GetAsync($"accounts({id})");
if (!accountResponse.IsSuccessStatusCode)
{
if (accountResponse.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return new NotFoundResult();
}

// throws Exception
accountResponse.EnsureSuccessStatusCode();
}

var accountJson = await accountResponse.Content.ReadAsStringAsync();
return new ContentResult()
{
Content = accountJson,
ContentType = "application/json"
};
}
catch (HttpRequestException ex)
{
_logger.LogError(ex, "An error has occured while processing Dynamics365-CRM-Accounts request.");
return new StatusCodeResult(ex.StatusCode.HasValue ? (int)ex.StatusCode.Value : StatusCodes.Status500InternalServerError);
}
}
}
}
2 changes: 1 addition & 1 deletion FunctionApp/Dynamics365/CRM/Authorize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public Authorize(IOptions<AppSettings> settings, ILogger<Authorize> logger)
}

[Function("D365-CRM-Authorize")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "crm/authorize")] HttpRequest req)
{
var scopes = new string[] { $"https://admin.services.crm.dynamics.com/user_impersonation", "offline_access" };

Expand Down
41 changes: 36 additions & 5 deletions FunctionApp/Dynamics365/CRM/Contacts.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Grpc.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
Expand All @@ -18,15 +19,45 @@ public Contacts(HttpClientProvider httpClientProvider, ILogger<Contacts> logger)
}

[Function("D365-CRM-Contacts")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req)
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "crm/contacts/{id?}")] HttpRequest req, Guid? id)
{
_logger.LogInformation("Dynamics365-CRM-Contacts is requested.");

var client = _httpClientProvider.Create();
var contactsJson = await client.GetStringAsync("contacts");
var contacts = JsonValue.Parse(contactsJson);
try
{
var client = _httpClientProvider.Create();

return new OkObjectResult(contacts?["value"]);
if (!id.HasValue)
{
var contactsJson = await client.GetStringAsync("contacts?$select=contactid,fullname");
var contacts = JsonValue.Parse(contactsJson);
return new OkObjectResult(contacts?["value"]);
}

var contactResponse = await client.GetAsync($"contacts({id})");
if (!contactResponse.IsSuccessStatusCode)
{
if (contactResponse.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return new NotFoundResult();
}

// throws Exception
contactResponse.EnsureSuccessStatusCode();
}

var contactJson = await contactResponse.Content.ReadAsStringAsync();
return new ContentResult()
{
Content = contactJson,
ContentType = "application/json"
};
}
catch (HttpRequestException ex)
{
_logger.LogError(ex, "An error has occured while processing Dynamics365-CRM-Contacts request.");
return new StatusCodeResult(ex.StatusCode.HasValue ? (int)ex.StatusCode.Value : StatusCodes.Status500InternalServerError);
}
}
}
}

0 comments on commit bde7f4f

Please sign in to comment.