Wednesday, February 8, 2017

SharePoint Online: Get List Items from Folder using PowerShell

Requirement: Get list items from a folder in SharePoint Online.

SharePoint Online: PowerShell to Get List Items in a Folder

#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


PowerShell to Get Files from Folder in SharePoint Online:
This time, lets retrieve all files from a given folder in SharePoint Online document library using PowerShell.
#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-FilesFromFolder()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [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 Folder and Files
        $Folder=$Ctx.Web.GetFolderByServerRelativeUrl($FolderURL)
        $Ctx.Load($Folder)
        $Ctx.Load($Folder.Files)
        $Ctx.ExecuteQuery()

        #Iterate through each File in the folder
        Foreach($File in $Folder.Files)
        {
            #Get Name for each File
            Write-Host $File.Name
        }
    }
    Catch {
        write-host -f Red "Error Getting Files from Folder!" $_.Exception.Message
    }
}

#Set Parameter Values
$SiteURL="https://crescent.sharepoint.com"
#Relative URL to the Folder - For Lists: "/Lists/ListURL/FolderURL"
$FolderURL="/Shared Documents/Documentation"

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

Tags: sharepoint online get all files from folder, sharepoint csom get folder items, sharepoint caml get items in folder, sharepoint online get all items in a folder, sharepoint online powershell get all items in folder, sharepoint client object model get folder items, sharepoint online get list items inside folder, get all items from sharepoint list folder, sharepoint powershell get items in folder, powershell sharepoint get files in folder



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...