Reputation: 238
I have this .NET 8.0 console application, which uses PnP core SDK to connect to SharePoint online:-
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PnP.Core.Auth;
using PnP.Core.Model.SharePoint;
using PnP.Core.QueryModel;
using PnP.Core.Services;
using PnP.Core.Services.Builder.Configuration;
using System;
using System.Security.Cryptography.X509Certificates;
namespace ConsoleApp4
{
internal class Program
{
static async Task Main(string[] args)
{
var tenantId = "";
var clientId = "*****************8";
var certificatePath = @"c:\CERT****.pfx";
var certificatePassword = "********************";
// Initialize a new service collection
var serviceCollection = new ServiceCollection();
// Load the certificate
var certificate = new X509Certificate2(certificatePath, certificatePassword, X509KeyStorageFlags.Exportable);
// Configure logging
//serviceCollection.AddLogging(builder =>
//{
// builder.AddConsole();
//});
// Add and configure PnP Core SDK
serviceCollection.AddPnPCore(options =>
{
options.PnPContext.GraphFirst = false; // Set true if you prefer to use Graph over CSOM when possible
// options.HttpRequests.UserAgent = "ISV|Contoso|ProductX";
options.Sites.Add("SiteToWorkWith", new PnPCoreSiteOptions
{
SiteUrl = "https://****.sharepoint.com/sites/****",
AuthenticationProvider = new X509CertificateAuthenticationProvider(clientId, tenantId, certificate)
});
});
// Build the service provider
var serviceProvider = serviceCollection.BuildServiceProvider();
// Use the service provider to get the IPnPContextFactory instance
var pnpContextFactory = serviceProvider.GetRequiredService<IPnPContextFactory>();
// Now you can use the IPnPContextFactory to get a PnPContext and perform operations
var context = await pnpContextFactory.CreateAsync("SiteToWorkWith");
// Assume the fields where not yet loaded, so loading them with the list
var myList = await context.Web.Lists.GetByTitleAsync("Work Orders", p => p.Title,
p => p.Fields.QueryProperties(p => p.InternalName,
p => p.FieldTypeKind,
p => p.TypeAsString,
p => p.Title));
int i = 0;
var t = "";
// Do a paged retrieval of the list items
await foreach (var listItem in myList.Items)
{
// Do something with the list item
t= listItem["Created"].ToString();
i++;
}
// Example operation: Print the title of the SharePoint site
// Explicitly load the Title property of the Web
await context.Web.LoadAsync(w => w.Title);
Console.WriteLine($"Site title: {context.Web.Title}"+ i);
// Ensure to dispose the context
context.Dispose();
}
}
}
but is there a way to show/trace the requests that get executed from my console application? so i can track how the console application interact with SharePoint behind the senses? so i can optimized it as needed?
Thanks
Upvotes: 0
Views: 50