0

I'm trying to display the versions of each item in a folder that contains sub folders.

Since I don't necessarily want to scan a whole SPO list, I specified to start in a certain folder, e.g. '/Documents/test'. If this starting folder contains a sub folder, e.g. '/Documents/test/subtest' I'm also able to retrieve it and its contents. However if this sub folder contains one more level my script doesn't retrieve it and its contents. I don't know what I can do to drill down recursively.

I appreciate any tips.

$site = "https://finnova-my.sharepoint.com/sites/contoso"  
$folderPath = "/Documents/test"

Connect-PnPOnline $site -Credentials $credential
$spoCred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials 
$credential.UserName, $credential.Password

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext $site
$ctx.Credentials = $spoCred

$web = $ctx.Web
$ctx.Load($web)
$ctx.ExecuteQuery()


$serverRelativeUrl = (Get-PnPList Documents).RootFolder.ServerRelativeUrl

$folder = $web.GetFolderByServerRelativeUrl($site+$folderPath)
$ctx.Load($folder)
$ctx.Load($folder.Folders)
$ctx.ExecuteQuery()

ForEach ($subFolder in $folder.Folders)
{
    $ctx.Load($subFolder.Files)
    $ctx.ExecuteQuery()

  ForEach ($file in $subFolder.Files)
  {
      $file.Name
      $version = $file.Versions
      $ctx.Load($version)
      $ctx.ExecuteQuery()
      $versionCount = $version.Count
      $versionCount
  }
}
  • Put your code in one function and call it recursively. Obvious you need bit modification in you code. – Dhaval Kariya Jul 11 '17 at 12:05
0

Have you tried programming recursively?

You could also make a list of folders to "work on"...

WHILE there are items in the WORKLIST
  take the first item off the work-list and call it WORKITEM
  FOREACH sub-items in WORKITEM
    IF sub-item is a ListItem 
      process the versions
    ELSE //it mus be a folder....
      append sub-item to WORKLIST

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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