kwizcom banner advertisement

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.
#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()
        [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

        #Frame CamlQuery to retrieve the items from the Folder
        $CAMLQuery= New-Object Microsoft.SharePoint.Client.CamlQuery
        #Set relative URL of the folder

        <#To get all files and folders from a given folder, also set view scope. Otherwise, it gets all files and folders ONLY from the given folder location (and not anything underneath!) 
        $CAMLQuery.ViewXml = "@
        <view scope='Recursive'></view>"

        #Get List Items from the Folder

        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

#Relative URL to the Folder - For Libraries, E.g: "/Documents/2018" or "/sites/sales/documents/projects/active"

#Call the function to get list items from folder
Get-ListItemsFromFolder -SiteURL $SiteURL -ListName $ListName -FolderURL $FolderURL

This SharePoint Online PowerShell gets all items in folder. Here is my another post to get files from a folder SharePoint Online: Get All Files from Folder

Tags: sharepoint caml get items in folder, sharepoint online get all items in a folder, sharepoint client object model get folder items, sharepoint powershell get items in folder
SharePoint Online: Get List Items from Folder using PowerShell SharePoint Online: Get List Items from Folder using PowerShell Reviewed by Salaudeen Rajack on February 08, 2017 Rating: 5

1 comment:

  1. Dear Salaudeen,

    how to apply OCR on pdf inside document library (folder and subfolders) of sharepoint online ?



Please Login and comment to get your questions answered!

Powered by Blogger.