0

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

1 Answer 1

1

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"]
   }

    }
2
  • My Document sets are inside folder.How can i get those from a folder?
    – Swetha
    Commented Jul 9, 2019 at 12:39
  • we can use pnp powershell. I have updated the reply.
    – Amy_MSFT
    Commented Jul 10, 2019 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.