Using PowerShell to connect to Project Online with CSOM under MFA

I am trying to interact with a PWA on a tenant for which I have no choice but to use MFA. I’m trying to use the OfficeDev-PnP library which supports MFA, but can’t get it to work with Project Online.

This works, but doesn’t support MFA

$siteURL = ""
$username = "[email protected]"
$password = Read-Host -Prompt "Enter password" -AsSecureString
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
# Create & configure a client context connection
$pctx.Credentials = $credentials
# ...load stuff

This uses Office Dev PnP to obtain a MFA-friendly auth context, but doesn’t work (I get a 403 on the $pctx.ExecuteQuery())

$authmgr = new-object OfficeDevPnp.Core.AuthenticationManager
$pctx = New-Object Microsoft.ProjectServer.Client.ProjectContext($siteUrl)
$pctx.Credentials = $authmgr.GetWebLoginClientContext($siteURL).Credentials
# ...load stuff

This latter method works fine for a plain vanilla SharePoint Online ClientContext. Maybe the problem is that $authmgr.GetWebLoginClientContext($siteURL) returns a ClientContext, not a ProjectContext.


  • Per Gautam’s suggestion, try to do $pctx = $authmgr.GetWebLoginClientContext($siteURL): this fails when executing a query to load projects, with the Cannot find an overload for “Load” and the argument count: “1” message. Here, I believe $pctx doesn’t know it’s supposed to be a ProjectContext and is still a ClientContext (thus nothing has exercised the ProjectServer CSOM).

For completeness, here’s the full code I’ve tried here:

# Load libraries
$PNPPath = "C:PathToPnP"
Add-Type –Path "$PNPPathMicrosoft.SharePoint.Client.dll"
Add-Type –Path "$PNPPathMicrosoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "$PNPPathMicrosoft.ProjectServer.Client.dll"
Add-Type -Path "$PNPPathOfficeDevPnP.Core.dll"

# do stuff
$siteURL = ""
$authmgr = New-Object OfficeDevPnp.Core.AuthenticationManager
$pctx = $authmgr.GetWebLoginClientContext($siteURL)    
$projects = $pctx.Projects
  • I also tried directly casting the ClientContext to a ProjectContext $pctx = [Microsoft.ProjectServer.Client.ProjectContext]$authmgr.GetWebLoginClientContext($siteURL) but this also fails, with a Cannot convert… error.

SharePoint Asked by sasfrog on January 1, 2021

1 Answers

One Answer

I don't know if there's another way to do this, but to get unstuck and do what I needed to do, I did the following:

  • Forked the repo
  • Created a new method in /Core/OfficeDevPnP/AuthenticationManager.cs based on GetWebLoginClientContext() that instead returns a ProjectContext instead of a ClientContext (obviously involved a couple of tweaks to the solution)
  • Built the solution and referenced the newly built OfficeDevPnP.Core.dll in my PowerShell code

$pctx = $authmgr.GetWebLoginProjectContext($siteURL)

Works like a charm.

Correct answer by sasfrog on January 1, 2021

Add your own answers!

Related Questions

How to Set up Search for Extended Web applications Sp2013

1  Asked on February 18, 2021 by allegro-octopus


Embed my website content in my sharepoint by URL

2  Asked on February 15, 2021 by hippyjim


Customized Form – Sharepoint Online library

1  Asked on February 14, 2021 by elised


Extract Excel file names into Sharepoint List

1  Asked on February 13, 2021 by madeline


Pinning a document to a SharePoint Home Page

1  Asked on February 11, 2021 by wrightyatwork


Recurring Work Flow

1  Asked on February 10, 2021 by curtis


Video views counter in SharePoint Online

1  Asked on February 3, 2021 by msiri28


Ask a Question

Get help from others!

© 2022 All rights reserved.