getting all lists within a subweb

%3CLINGO-SUB%20id%3D%22lingo-sub-71125%22%20slang%3D%22en-US%22%3Egetting%20all%20lists%20within%20a%20subweb%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-71125%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20trying%20to%20get%20all%20of%20the%20lists%20within%20a%20subweb%20%3A-%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGet-PnPLists%20-Web%20%3CA%20href%3D%22Https%3A%2F%2Fsometenant.sharepoint.com%2Fsites%2Fthis-site%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EHttps%3A%2F%2Fsometenant.sharepoint.com%2Fsites%2Fthis-site%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20keeps%20returning%20a%20FileNotFound%20error%20message.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20tried%3C%2FP%3E%3CP%3E%24subwebs%3D%20Get-PnPSubWebs%3C%2FP%3E%3CP%3Eforeach%20(%24subweb%20in%20%24subwebs)%3C%2FP%3E%3CP%3E%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Get-PnPLists%20-Web%20%24subweb.Url%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20still%20returns%20a%20FileNotFound%20error.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20am%20I%20doing%20wrong%20%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENigel%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-71125%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-71349%22%20slang%3D%22en-US%22%3ERe%3A%20getting%20all%20lists%20within%20a%20subweb%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-71349%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1992%22%20target%3D%22_blank%22%3E%40Nigel%20Price%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethis%20works%20when%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fmytenant.sharepoint.com%2Fsites%2Fsitename%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fmytenant.sharepoint.com%2Fsites%2Fsitename%3C%2FA%3E%20is%20a%20site%20collection%3C%2FSPAN%3E%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EConnect-PnPOnline%20https%3A%2F%2Fmytenant.sharepoint.com%2Fsites%2Fsitename%0A%24webs%3D%20Get-PnPSubWeb%0A%24web%20%3D%20%24web%5B1%5D%20%20%20%20%20%20%23%20%20(to%20select%20the%20first%20one)%0AGet-PnPList%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you%20then%20want%20to%20go%20to%20a%20subweb%20you%20will%20need%20to%20use%20something%20like%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EConnect-PnPOnline%20https%3A%2F%2Fmytenant.sharepoint.com%2Fsites%2Fsitename%0A%24webs%3D%20Get-PnPSubWeb%0A%24web%20%3D%20%24web%5B1%5D%20%20%20%20%20%20%23%20%20(to%20select%20the%20first%20one)%0AGet-PnPList%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOr%20of%20course%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EConnect-PnPOnline%20https%3A%2F%2Fmytenant.sharepoint.com%2Fsites%2Fsitename%0A%24web%20%3D%20Get-PnPSubWeb%20%7C%20where%20%7B%24_.Title%20-eq%20%22My%20Title%22%7D%0AGet-PnPList%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you%20need%20a%20subsubweb%20then%20you%20could%20do%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EConnect-PnPOnline%20https%3A%2F%2Fmytenant.sharepoint.com%2Fsites%2Fsitename%0A%24web%20%3D%20Get-PnPSubWeb%20%7C%20where%20%7B%24_.Title%20-eq%20%22My%20Title%22%7D%0A%24subweb%20%3D%20Get-PnPSubWeb%20%20-Web%20%24web%20%7C%20where%20%7B%24_.Title%20-eq%20%22My%20Sub%20Title%22%7D%0AGet-PnPList%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-71348%22%20slang%3D%22en-US%22%3ERe%3A%20getting%20all%20lists%20within%20a%20subweb%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-71348%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F54577%22%20target%3D%22_blank%22%3E%40Peter%20McDonald%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20is%20a%20subsite.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENigel%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-71346%22%20slang%3D%22en-US%22%3ERe%3A%20getting%20all%20lists%20within%20a%20subweb%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-71346%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F3694%22%20target%3D%22_blank%22%3E%40NarasimaPerumal%20Chandramohan%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eit%20looks%20as%20if%20this%20might%20be%20a%20timing%20problem.%26nbsp%3B%20If%20I%20run%20the%20two%20commands%20(Connect-PnPOnline%20and%20Get-PnPList)%20in%20a%20PowerShell%20(.ps1)%20file%20I%20get%20the%20error.%26nbsp%3B%20If%20I%20run%20the%20two%20commands%20using%20the%20PowerShell%20Gui%20and%20typing%20them%20in%20then%20all%20works%20well.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20anyway%20I%20can%20get%20the%20second%20command%20wait%20until%20the%20first%20command%20finishes%20(or%20even%20run%20on%20a%20single%20thread.)%20%3F%26nbsp%3B%20If%20this%20was%20JavaScript%20ot%20TypeScript%2C%20then%20I%20could%20use%20promises.%26nbsp%3B%20But%20in%20PowerShell%20%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENigel%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-71344%22%20slang%3D%22en-US%22%3ERe%3A%20getting%20all%20lists%20within%20a%20subweb%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-71344%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F3694%22%20target%3D%22_blank%22%3E%40NarasimaPerumal%20Chandramohan%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20%24lists%20%3D%20Get-PnPList%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tried%20that%20and%20when%20I%20went%20to%20access%20the%20%24lists%20I%20got%20this%20-%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eformat-default%20%3A%20The%20collection%20has%20not%20been%20initialized.%20It%20has%20not%20been%20requested%20or%20the%20request%20has%20not%20been%3CBR%20%2F%3Eexecuted.%20It%20may%20need%20to%20be%20explicitly%20requested.%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2B%20CategoryInfo%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3A%20NotSpecified%3A%20(%3A)%20%5Bformat-default%5D%2C%20CollectionNotInitializedException%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2B%20FullyQualifiedErrorId%20%3A%20Microsoft.SharePoint.Client.CollectionNotInitializedException%2CMicrosoft.PowerShell.Comma%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%20nds.FormatDefaultCommand%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENigel%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-71287%22%20slang%3D%22en-US%22%3ERe%3A%20getting%20all%20lists%20within%20a%20subweb%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-71287%22%20slang%3D%22en-US%22%3E%3CP%3ETry%20the%20below.%20Currently%20it%20seems%20-web%20is%20not%20working%2C%20alternatively%20you%20can%20connect%20to%20each%20subsite%20and%20then%20get%20the%20lists%20in%20those%20subsite.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24cred%3D%20Get-Credential%0Aconnect-pnponline%20-Url%20https%3A%2F%2Ftenantname.sharepoint.com%2Fsites%2Fcontosobeta%20-Credentials%20%24cred%0A%24subwebs%3DGet-PNPSubWebs%20-Recurse%20%20%20%0Aforeach(%24subweb%20in%20%24subwebs)%20%0A%7B%20%0AConnect-PNPonline%20-Url%20%24subweb.Url%20-Credentials%20%24cred%0AGet-PnPList%20%0A%7D%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-71243%22%20slang%3D%22en-US%22%3ERe%3A%20getting%20all%20lists%20within%20a%20subweb%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-71243%22%20slang%3D%22en-US%22%3EIs%20%3CA%20href%3D%22Https%3A%2F%2Fsometenant.sharepoint.com%2Fsites%2Fthis-site%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EHttps%3A%2F%2Fsometenant.sharepoint.com%2Fsites%2Fthis-site%3C%2FA%3E%20a%20new%20site%20collection%20or%20a%20subsite%20of%20%3CA%20href%3D%22Https%3A%2F%2Fsometenant.sharepoint.com%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EHttps%3A%2F%2Fsometenant.sharepoint.com%2F%3C%2FA%3E%20%3F%3C%2FLINGO-BODY%3E
Contributor

Hi

 

I am trying to get all of the lists within a subweb :-

 

Get-PnPLists -Web Https://sometenant.sharepoint.com/sites/this-site

 

This keeps returning a FileNotFound error message.

 

I have tried

$subwebs= Get-PnPSubWebs

foreach ($subweb in $subwebs)

{

    Get-PnPLists -Web $subweb.Url

}

 

This still returns a FileNotFound error.

 

What am I doing wrong ?

 

Regards

 

Nigel

6 Replies

Try the below. Currently it seems -web is not working, alternatively you can connect to each subsite and then get the lists in those subsite.

 

$cred= Get-Credential
connect-pnponline -Url https://tenantname.sharepoint.com/sites/contosobeta -Credentials $cred
$subwebs=Get-PNPSubWebs -Recurse   
foreach($subweb in $subwebs) 
{ 
Connect-PNPonline -Url $subweb.Url -Credentials $cred
Get-PnPList 
}

Hi @NarasimaPerumal Chandramohan

 

I have $lists = Get-PnPList

 

I tried that and when I went to access the $lists I got this ->

 

format-default : The collection has not been initialized. It has not been requested or the request has not been
executed. It may need to be explicitly requested.
    + CategoryInfo          : NotSpecified: (:) [format-default], CollectionNotInitializedException
    + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException,Microsoft.PowerShell.Comma
   nds.FormatDefaultCommand

 

Regards

 

Nigel

Hi @NarasimaPerumal Chandramohan

 

it looks as if this might be a timing problem.  If I run the two commands (Connect-PnPOnline and Get-PnPList) in a PowerShell (.ps1) file I get the error.  If I run the two commands using the PowerShell Gui and typing them in then all works well.

 

Is there anyway I can get the second command wait until the first command finishes (or even run on a single thread.) ?  If this was JavaScript ot TypeScript, then I could use promises.  But in PowerShell ?

 

Regards

 

Nigel

Hi @Peter McDonald

 

It is a subsite.

 

Regards

 

Nigel

Hi @Nigel Price

 

this works when https://mytenant.sharepoint.com/sites/sitename is a site collection:

 

 

Connect-PnPOnline https://mytenant.sharepoint.com/sites/sitename
$webs= Get-PnPSubWeb
$web = $web[1]      #  (to select the first one)
Get-PnPList

 

 

If you then want to go to a subweb you will need to use something like this:

 

 

Connect-PnPOnline https://mytenant.sharepoint.com/sites/sitename
$webs= Get-PnPSubWeb
$web = $web[1]      #  (to select the first one)
Get-PnPList

 

 

Or of course:

 

 

Connect-PnPOnline https://mytenant.sharepoint.com/sites/sitename
$web = Get-PnPSubWeb | where {$_.Title -eq "My Title"}
Get-PnPList

 

 

If you need a subsubweb then you could do this:

 

 

Connect-PnPOnline https://mytenant.sharepoint.com/sites/sitename
$web = Get-PnPSubWeb | where {$_.Title -eq "My Title"}
$subweb = Get-PnPSubWeb  -Web $web | where {$_.Title -eq "My Sub Title"}
Get-PnPList