Jul 02 2023 04:42 AM
I can able to loop till the subsites, but I am not able to get the current(subsite )Lists/doc lib. the below code is working fine till to get subsite details.
$SiteColl = "https://SharePoint-Contose/sites/Test" Connect-PnPOnline -Url $SiteColl -UseWebLogin $WebsCollection = Get-PnPSubWeb #Iterate through each subsite ForEach($Web in $WebsCollection) { Write-Host "Working On: " $Web.Url -BackgroundColor DarkGreen }
from here I would like to get current web lists/lib please find the code below which I tried.
ForEach($Web in $WebsCollection) { Write-host $Web.Url $PnPListColls = Get-PnPList #("here it get the Root site lists/lib always so updated like below") $PnPListColls = Get-PnPList -Web $Web } Here is the waring :WARNING: Parameter 'Web' is obsolete.
Jul 03 2023 02:32 AM
SolutionHello @Ganesh955,
To loop through the lists and list items in the subsites using PnP PowerShell, you can try to modify your code like this:
$SiteColl = "https://SharePoint-Contose/sites/Test"
Connect-PnPOnline -Url $SiteColl -UseWebLogin
$WebsCollection = Get-PnPSubWeb
# Iterate through each subsite
ForEach ($Web in $WebsCollection) {
Write-Host "Working On: " $Web.Url -BackgroundColor DarkGreen
# Connect to the current subsite
Connect-PnPOnline -Url $Web.Url -UseWebLogin
# Get the lists/libraries in the current subsite
$PnPListColls = Get-PnPList
# Loop through each list/library in the current subsite
ForEach ($List in $PnPListColls) {
Write-Host "List Title: " $List.Title
# Get the items in the current list/library
$ListItems = Get-PnPListItem -List $List.Title
# Loop through each item in the current list/library
ForEach ($Item in $ListItems) {
Write-Host "Item Title: " $Item.FieldValues.Title
}
}
}
This should loop through the sub-sites, retrieve the lists/libraries, and then loop through the items in each list/library.
Hope it helps.
iIf you think my answer helped you, you can click on Mark as best response.
Kindest regards
Leon Pavesic
Jul 03 2023 02:32 AM
SolutionHello @Ganesh955,
To loop through the lists and list items in the subsites using PnP PowerShell, you can try to modify your code like this:
$SiteColl = "https://SharePoint-Contose/sites/Test"
Connect-PnPOnline -Url $SiteColl -UseWebLogin
$WebsCollection = Get-PnPSubWeb
# Iterate through each subsite
ForEach ($Web in $WebsCollection) {
Write-Host "Working On: " $Web.Url -BackgroundColor DarkGreen
# Connect to the current subsite
Connect-PnPOnline -Url $Web.Url -UseWebLogin
# Get the lists/libraries in the current subsite
$PnPListColls = Get-PnPList
# Loop through each list/library in the current subsite
ForEach ($List in $PnPListColls) {
Write-Host "List Title: " $List.Title
# Get the items in the current list/library
$ListItems = Get-PnPListItem -List $List.Title
# Loop through each item in the current list/library
ForEach ($Item in $ListItems) {
Write-Host "Item Title: " $Item.FieldValues.Title
}
}
}
This should loop through the sub-sites, retrieve the lists/libraries, and then loop through the items in each list/library.
Hope it helps.
iIf you think my answer helped you, you can click on Mark as best response.
Kindest regards
Leon Pavesic