2

I am using the PnP modules to connect to a sharepoint site and retrieve its subsites and the lists within them. However when i try to use the below command it throws (403) forbidden error.

#Variables for Processing

$siteUrl = "https://*****.sharepoint.com/sites/PS1"
$siteAdminUrl = "https://****-admin.sharepoint.com/"

#Setup Credentials to connect
Write-Host -ForegroundColor Yellow "Setting up Creds"
#$userCredential= Get-Credential -UserName $adminUPN -Message "Password"
Write-Host -ForegroundColor Yellow "Connecting to Sharepoint online services"
Connect-SPOService -Url $siteAdminUrl
Connect-PnPOnline -Url $siteUrl -CurrentCredentials

$subsites = Get-PnPSubWebs
foreach ($subsite in $subsites) {

Write-Host $subsite.Title
$Lists = Get-PnPList
Write-Host -ForegroundColor Yellow "Subsite name is " $Subsite.Title" in the site"
Write-Host -ForegroundColor Green "There are totally " $Lists.count " lists available on the site"
foreach ($list in $Lists) {
Write-Host $list.Title
}
}

Thank you

1 Answer 1

1

Make sure that the User must be a O365 tenant administrator as well as site collection admin. I have updated your code and it works fine in my case.

#Variables for Processing

$siteUrl = "https://xxxxxx.sharepoint.com/sites/PS1"
$siteAdminUrl = "https://xxxxxxx-admin.sharepoint.com/"

#Setup Credentials to connect
Write-Host -ForegroundColor Yellow "Setting up Creds"
$userCredential= Get-Credential -UserName $adminUPN -Message "Password"
Write-Host -ForegroundColor Yellow "Connecting to Sharepoint online services"
Connect-SPOService -Url $siteAdminUrl
Connect-PnPOnline -Url $siteUrl -Credential $userCredential

$subsites = Get-PnPSubWebs
foreach ($subsite in $subsites) {

Write-Host $subsite.Title
$Lists = Get-PnPList
Write-Host -ForegroundColor Yellow "Subsite name is " $Subsite.Title" in the site"
Write-Host -ForegroundColor Green "There are totally " $Lists.count " lists available on the site"
foreach ($list in $Lists) {
Write-Host $list.Title
}
}

My Output Screen: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.