I'm trying to fetch all files of a document library in SharePoint Online Site. However this does not seem to work. I use the following code:
#Function to Download All Files from a SharePoint Online Folder - Recursively Function Download-SPOFolder([Microsoft.SharePoint.Client.Folder]$Folder, $DestinationFolder) {
#Get the Folder's Site Relative URL $FolderURL = $Folder.ServerRelativeUrl.Substring($Folder.Context.Web.ServerRelativeUrl.Length) $LocalFolder = $DestinationFolder + ($FolderURL -replace "/","\")
#Create Local Folder, if it doesn't exist If (!(Test-Path -Path $LocalFolder)) { New-Item -ItemType Directory -Path $LocalFolder | Out-Null Write-host -f Yellow "Created a New Folder '$LocalFolder'" }
#Get all Files from the folder $FilesColl = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL -ItemType File #Iterate through each file and download Foreach($File in $FilesColl) { Get-PnPFile -ServerRelativeUrl $File.ServerRelativeUrl -Path $LocalFolder -FileName $File.Name -AsFile -force
Write-host -f Green "`tDownloaded File from '$($File.ServerRelativeUrl)'" }
#Get Subfolders of the Folder and call the function recursively
$SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl
$FolderURL -ItemType Folder Foreach ($Folder in $SubFolders | Where {$_.Name -ne "Forms"}) { Download-SPOFolder $Folder $DestinationFolder } }
#Set Parameters
$SiteURL = "https://xxx.sharepoint.com/sites/tm_Finance"
$LibraryURL = "/Shared Documents/Budgets/" #Site Relative URL
$DownloadPath ="C:\Reports\"
#Connect to PnP Online Connect-PnPOnline -Url $SiteURL -UseWebLogin
#Get The Root folder of the Library
$Folder = Get-PnPFolder -Url $LibraryURL
#Call the function to download the document library Download-SPOFolder $Folder $DownloadPath
And I keep getting the following error code:
Get-PnPFolder : File Not Found. At line:1 char:11 + $Folder = Get-PnPFolder -Url $LibraryURL + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Get-PnPFolder], ServerException + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.GetFolder
However: the folder doest exist. I tried with different, existing folders but I still get the same error code.
Any ideas?