SharePoint Online: Create Document Set using PowerShell

Requirement: Create a Document Set in SharePoint Online using PowerShell

SharePoint Online: Create Document Set using PowerShell

While my other post explains how to create a document set in SharePoint Online: Creating Document Set in SharePoint Online – Step by Step, this blog post will show you how to use PowerShell to create a document set in SharePoint Online.

In short, A document set is an organizational container that holds one or more related documents. It’s like creating folders on your desktops for different projects and subfolders within those folders for individual tasks. This script assumes that the document sets feature is enabled, Content type for the document set is created and added to the library already. Here is the PowerShell script to create document sets in SharePoint Online:

#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"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.DocumentManagement.dll"

#Parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$DocumentLibraryName = "Proposal Documents"
$DocumentSetName = "Crescent Inc SharePoint"
#Content Type ID from the List!
$ContentTypeID="0x0120D52000683659260B26AF42AF65E57B09B613C2005AE31D539D0FB244BAEE6EA8CB1F4D52"

#Get Credentials to connect
$Cred= Get-Credential
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Get the Artifacts
    $DocumentLibrary=$Ctx.Web.Lists.GetByTitle($DocumentLibraryName)
    $Ctx.Load($DocumentLibrary)
    $Ctx.Load($DocumentLibrary.RootFolder)

    #Get content type from library by ID
    $ContentType = $DocumentLibrary.ContentTypes.GetById($ContentTypeID)
    $Ctx.Load($ContentType)
    $Ctx.ExecuteQuery()
 
    If($ContentType -ne $Null)
    {
        # sharepoint online powershell create document set from content type
        $DocumentSet = [Microsoft.SharePoint.Client.DocumentSet.DocumentSet]::Create($Ctx,$DocumentLibrary.RootFolder,$DocumentSetName,$ContentType.ID)
        $Ctx.ExecuteQuery()

        Write-host "Document Set '$DocumentSetName' Created Successfully!'" -f Green
    }
    else
    {
        Write-host "Content Type '$ContentTypeName' doesn't exist!'" -f Yellow
    }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

This script creates a new document set in SharePoint Online.

sharepoint online create document set powershell

PnP PowerShell to Create a Document Set in SharePoint Online

Here is how to create a document set in SharePoint Online using PnP PowerShell:

#Parameters
$SiteURL= "https://Crescent.sharepoint.com/sites/Docs"
$ListName = "Documents"
$ContentTypeName = "Invoice Doc Set"
$DocumentSetName = "Crescent Invoices"

#Connect to the Site
Connect-PnPOnline -Url $SiteURL -Interactive

#Create a Content Type from "Document Set" Parent content type
$ParentCType = Get-PnPContentType -Identity "Document Set"
$ContentType = Add-PnPContentType -Name $ContentTypeName -Group "Crescent" -ParentContentType $ParentCType

#Add the Content Type to Library
Add-PnPContentTypeToList -List $ListName -ContentType $ContentType

#Add Document Set to Library
Add-PnPDocumentSet -List $ListName -ContentType $ContentTypeName -Name $DocumentSetName

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

10 thoughts on “SharePoint Online: Create Document Set using PowerShell

  • I now have a different issue. I want to include the document set column values (custom columns assigned tot he content type created for the document set) in the creation process. I have tried using a Hashtable containing the docset properties. Afterwards, I simply added an argument to the end of the DocumentSet.Create method which references the hashtable as a fifth argument (I had previously found other examples of this on the internet). Is this the correct process? Now I get an error message – Error: Cannot find an overload for “Create” and the argument count: “5”. If I look at the described syntax for the DocumentSet.Create method, it mentions no first argument as being needed to specify the SP Client Context (which is what we are doing in the above example). How can I troubleshoot this? Thank-you.

    Reply
  • I keep getting the error message –

    Error: Cannot convert argument “ctid”, with value: “0x0120D520007C13A4BFEC96904182906261A074F784”, for “Create” to type “Microsoft.SharePoint.Client.ContentTypeId”: “Cannot convert the “0x0120D520007C13A4BFEC96904182906261A074F784” value of type “System.String” to type “Microsoft.SharePoint.Client.ContentTypeId”.”

    I know the ctID is correct. I have checked it again and again. Any ideas?

    Reply
    • I have resolved this issue now. No further support/action is needed.

      Reply
  • Where did you find this information ?

    $ContentTypeID=”0x0120D52000683659260B26AF42AF65E57B09B613C2005AE31D539D0FB244BAEE6EA8CB1F4D52″

    Reply
    • Thanks it work.

      Howerver I have 2 remaining problems:

      1st : It create it at the root of the Library. I want it to be created in a subfolder but I don’t understand how to add subfolder to the Folder parameters. Can you help me ?

      2nd : This document set have many metadata column which I have info in xls file. is it possible to create the documentset and then “push” the data in the column with the same script ?

      Thanks in advance,

      Reply
    • Build up a $parentFolder and pass it instead of $DocumentLibrary.RootFolder

      $parentFolder = Get-PnPFolderItem -FolderSiteRelativeUrl “Test AFV Library/01 Level” -ItemName “02 Level” -ItemType Folder
      write-host parentFolder.Name: $parentFolder.Name

      Reply
  • Hi

    On this line

    $DocumentSet = [Microsoft.SharePoint.Client.DocumentSet.DocumentSet]::Create($Ctx,$DocumentLibrary.RootFolder,$DocumentSetName,$ContentType.ID)
    $Ctx.ExecuteQuery()

    I get the error

    Cannot convert the “Microsoft.SharePoint.Client.ClientContext” value of type “Microsoft.SharePoint.Client.ClientContext” to type “Microsoft.S
    harePoint.Client.ClientRuntimeContext”

    Any ideas?

    Thanks
    P

    Reply
    • Uninstall the previous version of SharePoint Online Client Components SDK & SharePoint Online Management Shell (If installed), Download and install the latest version!

      Reply
  • Hi Salaudeen,

    i need to create Document set inside subfolder (e.g i want to move a file XYZ.doc in a document set inside Test Library, document set name should be file name if document set not exist it should create and then move the file into it )
    Thanks in Advance

    Reply

Leave a Reply

Your email address will not be published.