Forum Discussion

RichardS's avatar
RichardS
Copper Contributor
Aug 04, 2023
Solved

403 with a ctx.load($WEB) executeQuery

I have a powershell script which is failing with a 403 Forbidden.  I have tried 2 variants of Connect-PnPOnline

 

 $SiteURL = “https://MyDomain.sharepoint.com/sites/MySPSite/”

Connect-PnPOnline -Url $SiteURL -Interactive

#      Connect-PnPOnline -Url $SiteURL -UseWebLogin

 

$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) 

$Web = $Ctx.Web 

$Ctx.Load($Web) 

$Ctx.Load($Web.AllProperties) 

$Ctx.ExecuteQuery() 

 

The ExecuteQuery returns a 403 forbidden. 

 

I am in the ‘Site Owners’ Group for https://MyDomain.sharepoint.com/sites/MySPSite

I have the SharePoint Administrator role activated in Portal.Azure.Com >> Azure AD roles

 

What am I missing ? All suggestions gratefully received. Thank you

  • Hi RichardS ,

    If you want to use the authentication provided by PnP Powershell, then you need to get the context using the "Get-PnPContext" command

    $Ctx=Get-PnPContext
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.Load($Web.AllProperties)
    $Ctx.ExecuteQuery()


    If you call 

    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) 

    you create a context object that has not yet been authenticated.

    Best Regards,
    Sven

2 Replies

  • SvenSieverding's avatar
    SvenSieverding
    Bronze Contributor

    Hi RichardS ,

    If you want to use the authentication provided by PnP Powershell, then you need to get the context using the "Get-PnPContext" command

    $Ctx=Get-PnPContext
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.Load($Web.AllProperties)
    $Ctx.ExecuteQuery()


    If you call 

    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) 

    you create a context object that has not yet been authenticated.

    Best Regards,
    Sven

    • RichardS's avatar
      RichardS
      Copper Contributor
      Many Thanks . That makes perfect sense

Resources