How to fix

Aron Beyens 0 Reputation points
2023-08-29T14:01:44.95+00:00

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?

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,956 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,682 questions
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 36,731 Reputation points Microsoft Vendor
    2023-08-30T02:58:55.1233333+00:00

    Hi @Aron Beyens,

    Per my test, the method Get-PnPFolder is to get the folders in document library. The $LibraryURL should be the url of the library. In your case it should be like following, please make a reference

    $LibraryURL = "Shared Documents"
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.