0

I have a question regarding the PnP Powershell and the usage.

So basically, I've created a simple foreach to iterate all subwebs to print a detail froma list. But it does not work as supposed. It doesn't do anything.

$webs = Get-PnPSubWebs -recurse -IncludeRootWeb -Includes "Lists";

    foreach($web in $webs)
    {
        foreach ($item in web.Lists["MyCustomList"].Items)
        {
            write-host $item.Title;
        }
      }

I've tried to reproduce everything step by step in the Powershell Window:

  1. Ok it gives me all webs with lists (firstly I didn't know I had to order lists to be included)

    $webs = Get-PnPSubWebs -Recurse -IncludeRootWeb -Includes "Lists"

  2. This shows me all the lists

    $webs.Lists

  3. But I'm stuck here:

    $web.Lists.GetByTitle("MyCustomList")

This returns nothing! I've tried GetById and everything but it doens't work. I could only get a list like this: $web.Lists[20] (with index)

Could you please assist, so that I can interate through webs, find my list and iterate through all the items in my list. (this is for sharepoint 2016)

2 Answers 2

0

It's likely that you will need to get the list object itself using Get-PnPList

https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/get-pnplist?view=sharepoint-ps

0

SharePoint CSOM APIs are currently not supported in PnP PowerShell. You cannot use ListCollection.GetByTitle method in PnP PowerShell.

You can use Get-PnPListItem for a known list in your site:

#Config Variables
$SiteUrl = "<SiteURL>"
$Username = "<Domain\user>"
$Password = "<Password>"
$ListName = "<ListName>"

#Connect to PNP Online
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName, $(convertto-securestring $Password -asplaintext -force)
Connect-PnPOnline -Url $SiteUrl -Credential $cred

#sharepoint online pnp powershell get list items
$ListItems = Get-PnPListItem -List $ListName -Fields "Title"
 
#Loop through each Item
foreach($ListItem in $ListItems) { 
    Write-Host "Title:" $ListItem["Title"]
}

Reference: SharePoint Online: Get All List Items using PowerShell Script.

Your Answer

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

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