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.ViewXml = '<View><Query><Where><Eq><FieldRef Name=''Title'' /><Value Type=''Text''>' + $directorate + '</Value></Eq></Where></Query></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
}
}