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

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.