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

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.