diff --git a/FunctionApp/SharePoint/GraphServiceClientProvider.cs b/FunctionApp/SharePoint/GraphServiceClientProvider.cs index 24ad756..95c09b7 100644 --- a/FunctionApp/SharePoint/GraphServiceClientProvider.cs +++ b/FunctionApp/SharePoint/GraphServiceClientProvider.cs @@ -3,6 +3,7 @@ using Microsoft.Graph; using Microsoft.Identity.Client; using Plumsail.DataSource.SharePoint.Settings; +using System.Net.Http; namespace Plumsail.DataSource.SharePoint { @@ -24,6 +25,11 @@ public GraphServiceClient Create() }; var clientSecretCredential = new ClientSecretCredential(_azureAppSettings.Tenant, _azureAppSettings.ClientId, _azureAppSettings.ClientSecret, options); + + // for debugging requests + //var debugHandler = new DebugRequestHandler(new DebugResponseHandler()); + //var httpClient = new HttpClient(debugHandler); + //return new GraphServiceClient(httpClient, clientSecretCredential); return new GraphServiceClient(clientSecretCredential); } } diff --git a/FunctionApp/SharePoint/ListData.cs b/FunctionApp/SharePoint/ListData.cs index 13dcc08..9c664c7 100644 --- a/FunctionApp/SharePoint/ListData.cs +++ b/FunctionApp/SharePoint/ListData.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Options; using Microsoft.Graph; using Microsoft.Graph.Models; +using Microsoft.Graph.Models.ODataErrors; using System.Collections.Generic; using System.Threading.Tasks; using static System.Runtime.InteropServices.JavaScript.JSType; @@ -27,29 +28,47 @@ public ListData(IOptions settings, GraphServiceClientProvi [Function("SharePoint-ListData")] public async Task Run( - [HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req) + [HttpTrigger(AuthorizationLevel.Function, "get", Route = "sp/items/{id?}")] HttpRequest req, int? id) { - _logger.LogInformation("ListData is requested."); + _logger.LogInformation("SharePoint-ListData is requested."); var graph = _graphProvider.Create(); var list = await graph.GetListAsync(_settings.SiteUrl, _settings.ListName); - var itemsPage = await list.Items.GetAsync(requestConfiguration => + if (list == null) { - //requestConfiguration.QueryParameters.Filter = "fields/Title eq 'item 1'"; - requestConfiguration.QueryParameters.Select = ["id"]; - requestConfiguration.QueryParameters.Expand = ["fields($select=Title,Author)"]; - }); - - var items = new List(); - var pageIterator = PageIterator - .CreatePageIterator(graph, itemsPage, item => + return new NotFoundResult(); + } + + if (!id.HasValue) + { + var itemsPage = await list.Items.GetAsync(requestConfiguration => { - items.Add(item); - return true; + //requestConfiguration.QueryParameters.Filter = "fields/Title eq 'item 1'"; + requestConfiguration.QueryParameters.Select = ["id"]; + requestConfiguration.QueryParameters.Expand = ["fields($select=Title,Author)"]; }); - await pageIterator.IterateAsync(); - return new OkObjectResult(items); + var items = new List(); + var pageIterator = PageIterator + .CreatePageIterator(graph, itemsPage, item => + { + items.Add(item); + return true; + }); + + await pageIterator.IterateAsync(); + return new OkObjectResult(items); + } + + try + { + var item = await list.Items[id.ToString()].GetAsync(); + return new OkObjectResult(item?.Fields?.AdditionalData); + } + catch (ODataError error) when (error.ResponseStatusCode.Equals(404)) + { + return new NotFoundResult(); + } } } }