SharePoint Online: Get List Items from Folder using PowerShell
Requirement: Get all list items from a folder in SharePoint Online.
SharePoint Online: PowerShell to Get List Items in a Folder
Get all items from SharePoint list folder is a common requirement when working with client side object model scripts. Here is the SharePoint CSOM to get folder items.
To get all files and folders from a given folder, you must set view scope. Otherwise, it gets all files and folders ONLY from the given folder location and not anything underneath!
SharePoint Online: Get All Items in a Folder using PnP PowerShell
Here is the SharePoint CAML to get items in folder.
SharePoint Online: PowerShell to Get List Items in a Folder
Get all items from SharePoint list folder is a common requirement when working with client side object model scripts. Here is the SharePoint CSOM to get folder items.
#Load SharePoint CSOM Assemblies Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" Function Get-ListItemsFromFolder() { param ( [Parameter(Mandatory=$true)] [string] $SiteURL, [Parameter(Mandatory=$true)] [string] $ListName, [Parameter(Mandatory=$true)] [string] $FolderURL ) Try { #Setup Credentials to connect $Cred = Get-Credential $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password) #Setup the context $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) $Ctx.Credentials = $Cred #Get the list $List=$Ctx.Web.Lists.GetByTitle($ListName) #Frame CamlQuery to retrieve the items from the Folder $CAMLQuery= New-Object Microsoft.SharePoint.Client.CamlQuery #Set relative URL of the folder $CAMLQuery.FolderServerRelativeUrl=$FolderURL #Get List Items from the Folder $ListItems=$List.GetItems($CAMLQuery) $Ctx.Load($ListItems) $Ctx.ExecuteQuery() Write-host "Total Number of Items Found:"$ListItems.Count #Iterate through all list items Foreach($Item in $ListItems) { #Get Ids for each Item Write-Host $item["ID"] } } Catch { write-host -f Red "Error Getting List Items from Folder!" $_.Exception.Message } } #Set Parameter Values $SiteURL="https://crescent.sharepoint.com" $ListName="Projects" #Relative URL to the Folder - For Libraries, E.g: "/Documents/2018" or "/sites/sales/documents/projects/active" $FolderURL="/Lists/Projects/Active" #Call the function to get list items from folder Get-ListItemsFromFolder -SiteURL $SiteURL -ListName $ListName -FolderURL $FolderURLThis SharePoint Online PowerShell gets all items in folder. If you want to get all files from a document library, use: PowerShell to get list of files in document library in SharePoint Online
To get all files and folders from a given folder, you must set view scope. Otherwise, it gets all files and folders ONLY from the given folder location and not anything underneath!
$CAMLQuery.ViewXml = "<View Scope='RecursiveAll' /><Where><Eq><FieldRef Name='ServerRelativeUrl'/><Value Type='Text'>/Documents/Archived/2018</Value></Eq></Where>";
SharePoint Online: Get All Items in a Folder using PnP PowerShell
Here is the SharePoint CAML to get items in folder.
#Config Variables $SiteURL = "https://crescenttech.sharepoint.com" $ListName = "Projects" $FolderRelativeURL= "/Lists/Projects/2018" #Get Credentials to connect $Cred = Get-Credential Try { #Connect to PNP Online Connect-PnPOnline -Url $SiteURL -Credentials $Cred #Get All Items from the Folder $CAMLQuery = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FileDirRef'/><Value Type='Text'>$FolderRelativeURL</Value></Eq></Where></Query></View>" $FolderItems = Get-PnPListItem -List $ListName -Query $CAMLQuery Write-host "Total Number of Items in the Folder:" $FolderItems.Count ForEach($Item in $FolderItems) { Write-host $Item["Title"] } } catch { write-host "Error: $($_.Exception.Message)" -foregroundcolor Red }You can also use Get-PnPFolderItem cmdlet with the URL fo the folder to retrieve all items from a folder or sub-folder:
Get-PnPFolderItem -FolderSiteRelativeUrl "/Shared Documents/New"This gets all items in a folder in SharePoint Online. Here is my another post to get files from a folder SharePoint Online: Get All Files from Folder
Dear Salaudeen,
ReplyDeletehow to apply OCR on pdf inside document library (folder and subfolders) of sharepoint online ?
Regards
Office 365 supports OCR by default! https://techcommunity.microsoft.com/t5/microsoft-search-blog/search-for-words-in-your-images-in-office-365/ba-p/135703
DeleteMy experience with scanned PDF documents is not working always. However, confirmed its working when I set "Make PDFs Readable" option in PDF editors such as Foxit PDF.