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()
{
    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

        <#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
        $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 $FolderURL
This SharePoint Online PowerShell gets all items in folder.

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
}
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
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 ?

    Regards

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.