I have to get all document sets from a library using its content type id, using CSOM Powershell. How to acheive this?Can anyone post some links related to this?
Regards Swetha
Yes, you can get all document sets from one library by PowerShell script in SharePoint online.
The content type id of the document set is 0x0120D52000364A67128FA90B4FAA5D470A057AEC22.
There is a demo below for your reference.
$SiteUrl = "https://yoursite "
$UserName = "username"
$Password = "password"
$ListName="documentlibraryname"
$SecurePassword= ConvertTo-SecureString $Password –asplaintext –force
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Context.Credentials = $Credentials
$List = $Context.web.Lists.GetByTitle($ListName)
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Context.Load($ListItems)
$Context.ExecuteQuery()
foreach($item in $ListItems)
{
if($item["ContentTypeId"] -like "0x0120D52000*")
{
write-host $item["ID"]
write-host $item["Title"]
write-host $item["ContentTypeId"]
}
}
updated:
$SiteURL = "https://yourtenant.sharepoint.com/sites/yoursite"
$ListName = "libraryname"
$FolderRelativeURL= "/sites/sitename/libraryname/foldername"
#Get Credentials to connect
$Cred = Get-Credential
#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)
{
if($Item["ContentTypeId"] -like "0x0120D52000*")
{
write-host $Item["ID"]
write-host $Item["Title"]
write-host $Item["ContentTypeId"]
}
}