🔐 Connecting to SharePoint Online Using PnP Core SDK and Interactive Login in .NET

Author: Edvaldo Guimarães\ Category: Microsoft 365 Development / C# / SharePoint\ Tags: .NET 6, PnP Core SDK, MSAL, SharePoint Online, Authentication


🧠 Introduction

In this article, we’ll walk through how to build a simple .NET console application that connects to SharePoint Online using the PnP Core SDK and interactive login via MSAL. This setup is ideal for developers who want to automate SharePoint tasks securely using modern authentication.


🛠️ Prerequisites

Before you begin, make sure you have:

  • Visual Studio 2022 with .NET 6 or later
  • A registered Azure AD App with:
    • Client ID
    • Tenant ID
    • Delegated permission: Sites.ReadWrite.All
  • Access to a SharePoint Online site

🚀 Step-by-Step Implementation

1. Create a Console App

In Visual Studio:

  • Create a new project → Console App (.NET 6 or higher)
  • Name it SharePointUploader

2. Install Required NuGet Packages

Install the following packages via NuGet Package Manager:

PnP.Core
PnP.Core.Auth

These packages provide access to SharePoint Online and authentication via MSAL.


3. Configure Authentication and Connect to SharePoint

Here’s the final working code:

using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PnP.Core.Auth;
using PnP.Core.Services;

class Program
{
    static async Task Main(string[] args)
    {
        var services = new ServiceCollection();

        string clientId = "YOUR_CLIENT_ID";
        string tenantId = "YOUR_TENANT_ID";
        string siteUrl = "https://YOUR_DOMAIN.sharepoint.com/sites/YOUR_SITE";

        // Register PnP Core SDK services
        services.AddPnPCore();
        services.AddPnPCoreAuthentication();

        // Register interactive authentication provider
        services.AddScoped<IAuthenticationProvider>(sp =>
            new InteractiveAuthenticationProvider(
                clientId,
                tenantId,
                new Uri("http://localhost")
            )
        );

        var serviceProvider = services.BuildServiceProvider();
        var contextFactory = serviceProvider.GetRequiredService<IPnPContextFactory>();

        using var context = await contextFactory.CreateAsync(siteUrl);

        Console.WriteLine("✅ Successfully connected to SharePoint!");
    }
}


✅ What This Code Does

  • Sets up dependency injection for PnP Core SDK
  • Uses MSAL for interactive login
  • Connects to a SharePoint Online site using the provided URL
  • Prints a success message once connected

🔄 Next Steps

Now that you’re connected to SharePoint, you can:

  • Access lists and items
  • Upload files as attachments
  • Automate provisioning and metadata updates

In the next phase, we’ll extend this app to list files modified in the last hour and upload them as attachments to a SharePoint list item.


📌 Conclusion

Using the PnP Core SDK with interactive login is a clean and secure way to connect to SharePoint Online in modern .NET applications. This approach avoids legacy authentication and aligns with Microsoft 365 best practices.


Edvaldo Guimrães Filho Avatar

Published by