Forum Discussion

Nigel Price's avatar
Nigel Price
Iron Contributor
May 18, 2017

getting all lists within a subweb

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 
    }
    • Nigel Price's avatar
      Nigel Price
      Iron Contributor

      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

    • Nigel Price's avatar
      Nigel Price
      Iron Contributor

      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

      • Pieter Veenstra's avatar
        Pieter Veenstra
        MVP

        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

         

         

Resources