I had recently posted here regarding my task to move Document Libraries across sites. Since then I started learning Powershell and I am getting the grip, albeit in a slow manner. I have managed to create a list, add fields to it, and move the data across sites. The sites are within the same Office 365 but are different site collections.

Now to describe my actual task and therefore my aim; I have a document library, whose hierarchy looks something like this:

  • Document Library - Scanned Docs (1)
  • Document Sets - Representing Clients (1000)
  • Documents - Representing Invoices (100,000)

i.e. 1 Document Library, around 100 document sets, and around 100,000 documents. This means that each Document set contains an average of 100 documents, which represent invoices.

Having managed to fiddle with lists, I am now trying to do the same but with Document Sets. If I could somehow get into the document library and traverse the whole list of document sets, and then go inside each document set and handle each document, that would be awesome.

My issue is this, with lists, I created a context using the root URL of the Sharepoint site where the list resides, and the follow line of code gets the list by its name:


However, there is no equivalent for Document Sets.

What I found is this:

See link

It takes 'context' and 'folder' as parameters. I have no clue what to put in the Folder place. Should the context be created on the root folder's url and let the document library represent the 'folder'? I don't think this would make sense as a Document Library is more like a list than a 'folder'.

I also found this (c# code):

See link

I tried to transform the code into Powershell:

$ListItem = Microsoft.Sharepoint.Client.DocumentSet.GetDocumentSet(...); //     DocumentSet item

$documentSet = Microsoft.Sharepoint.Client.DocumentSet.GetDocumentSet($Context, $ListItem.Folder);

Well am I on the right track? What goes instead of the '...'?

Question 2: After being able to retrieve a document set I would need to somehow manage to traverse the document library and get the document sets one by one. Furthermore, I would need to go into each document set and get the documents one by one.

How do I do this? It seems practically an impossible task to find examples and therefore any help will be greatly appreciated. All I need is a kick start and I will take it from there.

1 Answer 1


This code loads a documentset based on a query for listitems by contenttype: I hope this helps.

$spQuery=New-Object Microsoft.SharePoint.Client.CamlQuery 
$caml = "<View><Query><Where><Eq><FieldRef Name='ContentType'/><Value Type='Computed'>"+$ContentType+"</Value></Eq></Where></Query></View>";
Write-Host $caml
$spQuery.ViewXml = $caml

#$spqQuery.ViewFields = "<FieldRef Name='FileLeafRef' /><FieldRef Name='Title' />" 
#$spqQuery.ViewFieldsOnly = $true 
$listItems= $List.GetItems($spQuery) 


Write-Host $listItems.Count

foreach($listItem in $listItems)  
    Write-Host "ID - " $listItem.ID "Title - " $listItem["Title"] " folder - " $listItem.Folder.Path

    $documentSet=New-Object Microsoft.SharePoint.Client.DocumentSet.DocumentSet($Context, $listItem.Folder.Path)
    Write-Host $documentSet.Path


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.