C# Auth x Sharepoint

/*
=================================================================================================           
Setup
=================================================================================================
Update NuGet Package Sources: dotnet nuget list source
Install default feeds: dotnet nuget add source https://api.nuget.org/v3/index.json -n "nuget.org"
Install NuGet package: dotnet add package Microsoft.Identity.Client
*/

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Identity.Client;
 
namespace AzureAuthExample
{
    class Program
    {
       
        private static string clientId = "243aa9af-placeholder-dsp";
        private static string tenantId = "b21-placeholder-ed26";
        private static string username = "placeholder@jabil.com";
        private static string password = "placeholder";
 
        static async Task Main(string[] args)
        {
            string[] scopes = { "https://graph.microsoft.com/.default" };
 
            var app = PublicClientApplicationBuilder
                .Create(clientId)
                .WithAuthority(new Uri($"https://login.microsoftonline.com/{tenantId}"))
                .Build();
 
            var securePassword = new System.Security.SecureString();
            foreach (char c in password) securePassword.AppendChar(c);
 
            var authResult = await app.AcquireTokenByUsernamePassword(scopes, username, securePassword).ExecuteAsync();
 
            // Part 1: Display the access token
            Console.WriteLine($"Access Token: {authResult.AccessToken}");
            Console.WriteLine("=====");
 
            var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {authResult.AccessToken}");
 
            // Part 2: Get the output of "https://graph.microsoft.com/v1.0/sites?search=*"
            var response = await httpClient.GetAsync("https://graph.microsoft.com/v1.0/sites?search=*");
            response.EnsureSuccessStatusCode();
            var sitesContent = await response.Content.ReadAsStringAsync();
            Console.WriteLine(sitesContent);
            Console.WriteLine("=====");
 
            // Part 3: Get the content of the "IO Management" list
            // https://jabil.sharepoint.com/sites/Finance_PowerAutomate/Lists/IO%20Management/AllItems.aspx
            // Get siteId via: https://jabil.sharepoint.com/sites/Finance_PowerAutomate/_api/site/id
            var siteId = "3c393991-f5d4-4ff3-96e7-a88cd1f09068";
            var listId = "bd9522e8-058d-43da-b150-1bd38d9e001f";
 
            response = await httpClient.GetAsync($"https://graph.microsoft.com/v1.0/sites/{siteId}/lists/{listId}/items?expand=fields");
            response.EnsureSuccessStatusCode();
            var listItemsContent = await response.Content.ReadAsStringAsync();
 
            // Parse the JSON response
            var listItemsData = System.Text.Json.JsonDocument.Parse(listItemsContent);
            var listItems = listItemsData.RootElement.GetProperty("value").EnumerateArray();
 
            Console.WriteLine("IO Management List Items:");
            Console.WriteLine("-------------------------");
 
            int index = 1;
            foreach (var item in listItems)
            {
                var fields = item.GetProperty("fields");
 
                Console.WriteLine($"Item {index}:");
                Console.WriteLine($"  Category: {(fields.TryGetProperty("Category", out var category) ? category.GetString() : "empty")}");
                Console.WriteLine($"  Profit Center: {(fields.TryGetProperty("ProfitCentre", out var profitCenter) ? profitCenter.GetString() : "empty")}");
                Console.WriteLine($"  Customer PO: {(fields.TryGetProperty("CustomerPO", out var customerPO) ? customerPO.GetString() : "empty")}");
                Console.WriteLine($"  Project Name: {(fields.TryGetProperty("ProjectName", out var projectName) ? projectName.GetString() : "empty")}");
                Console.WriteLine($"  Description: {(fields.TryGetProperty("PurchaseItem", out var description) ? description.GetString() : "empty")}");
                Console.WriteLine($"  NPI/MP: {(fields.TryGetProperty("Status", out var npimp) ? npimp.GetString() : "empty")}");
                Console.WriteLine($"  Profit %: {(fields.TryGetProperty("Profit_x0025_", out var profit) ? profit.GetDouble().ToString() : "empty")}");
                Console.WriteLine($"  VAT %: {(fields.TryGetProperty("VAT_x0025_", out var vat) ? vat.GetString() : "empty")}");
                Console.WriteLine($"  PO Amount: {(fields.TryGetProperty("POAmount", out var poAmount) ? poAmount.GetDouble().ToString() : "empty")}");
 
                Console.WriteLine($"  ID: {item.GetProperty("id").GetString()}");
                Console.WriteLine($"  Created By: {item.GetProperty("createdBy").GetProperty("user").GetProperty("displayName").GetString()}");
                Console.WriteLine($"  Created DateTime: {item.GetProperty("createdDateTime").GetString()}");
                Console.WriteLine($"  Last Modified By: {item.GetProperty("lastModifiedBy").GetProperty("user").GetProperty("displayName").GetString()}");
                Console.WriteLine($"  Last Modified DateTime: {item.GetProperty("lastModifiedDateTime").GetString()}");
                Console.WriteLine($"  Web URL: {item.GetProperty("webUrl").GetString()}");
                Console.WriteLine();
 
                index++;
            }
        }
    }
}

Last updated