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 another post explains Creating Document Set in SharePoint Online - Step by Step, here is the PowerShell script to create document sets in SharePoint Online. This script assumes that the document sets feature is enabled, Content type for document set is created and added to the library already.
#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 creates a new document set in SharePoint Online.
sharepoint online create document set powershell

SharePoint Online: Create Document Set using PowerShell SharePoint Online: Create Document Set using PowerShell Reviewed by Salaudeen Rajack on August 16, 2018 Rating: 5

6 comments:

  1. 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

    ReplyDelete
  2. 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

    ReplyDelete
    Replies
    1. Uninstall the previous version of SharePoint Online Client Components SDK & SharePoint Online Management Shell (If installed), Download and install the latest version!

      Delete
  3. Where did you find this information ?

    $ContentTypeID="0x0120D52000683659260B26AF42AF65E57B09B613C2005AE31D539D0FB244BAEE6EA8CB1F4D52"

    ReplyDelete
    Replies
    1. 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,

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.