Powershell script to create multiples document sets in SharePoint online

 This script doesn't create multiples document set but any content type inherited from Document set. We just need to replace the Content type ID

It also sets their properties. SharePoint treats Document set as Folder, so all we need is to load all folder's properties by .ListItemAllFields, then set values.

Assume that we have a csv file which contains name and other properties of Document set, like this:

Here is the script:

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"

FunctionCreate-DocumentSet([Microsoft.SharePoint.Client.ClientContext]$Context, [String]$ListTitle,[String]$DocSetName){

  $list = $Context.Web.Lists.GetByTitle($listTitle)

  $docSetContentType = $Context.Site.RootWeb.ContentTypes.GetById("0x0120D52000630DEF4DF8BE444B9D9C9653F6221907")

  $Context.Load($docSetContentType)

  $Context.ExecuteQuery()

  $result = [Microsoft.SharePoint.Client.DocumentSet.DocumentSet]::Create($Context, $list.RootFolder, $DocSetName, $docSetContentType.Id);	

	$Context.ExecuteQuery()

  return $result.Value 				

	}


$UserAdmin = "***@xxx.onmicrosoft.com"

$SiteURL = "Your site URL"

$Password = "*****"

$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserAdmin,(ConvertTo-SecureString $Password -AsPlainText -Force))

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)

$Context.Credentials = $Creds


$Source = Import-CSV "D:\Testfile.csv"
foreach ($row in $Source)

{ 

$dsname = $row.'Name'

$docSetUrl = Create-DocumentSet -Context $context -ListTitle "Documents" -DocSetName $dsname

$DocSetRelativeUrl = "/sites/test/Shared Docuemnts/"+$dsname

$CurrentDocSet = $Context.web.GetFolderByServerRelativeUrl($DocSetRelativeURL)

$Context.Load($CurrentDocSet)

$Context.ExecuteQuery()



$DsProperty = $CurrentDocSet.ListItemAllFields

$DsProperty["Project"] = $row.'Project'

$DsProperty["Client"] = $row.'Client'

$DsProperty["Supplier"] = $row.'Supplier'IF(![string]::IsNullOrEmpty($row.'Date'))

{

$DsProperty["DateDS"] = $row.'Date'

}

$DsProperty["DateIsDue" = $row.'DateIsDue'

$DsProperty["Authorization"] = $row.'Authorization'

$DsProperty.Update()

$Context.Load($DsProperty)

$Context.ExecuteQuery()

write-host "$[$dsname] was created done" -foregroundcolor yellow 

}



$context.Dispose()

Comments