Tuesday, December 29, 2015

Creating a Document Set in a Document Library on SharePoint Online Using CSOM and PowerShell

Comments are in the script...

# Location of CSOM assemblies
# (from OfficeDev repo on GitHub: http://ift.tt/1MFijPt)

Set-Location "C:\Users\pchoquette\Source\Repos\PnP-Sites-Core\Assemblies\16"

# Generally required for any CSOM operations
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.Runtime.dll")

# Required for CSOM operations involving Document Sets
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.DocumentManagement.dll"

# Modify the details in this section to correspond to your Web on SPO
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext("http://ift.tt/22wRMiZ")

$password = Read-Host -Prompt "Enter password" -AsSecureString
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("pchoquette@starcrossed.ninja", $password)

# Modify this line with the title of your Document Library
$list = $ctx.Web.Lists.GetByTitle("Documents")

$ctx.Load($list)

# You could modify this line if you want the Document Set to go into a subfolder instead
$parentFolder = $list.RootFolder

$listContentTypes = $list.ContentTypes
$ctx.Load($listContentTypes)

# Modify this line with the Content Type ID for your Document Set
# You can get this by going into the Content Type Settings and copying it from the URL of that page

$myContentType = $listContentTypes.GetByID("0x0120D520000535852D838B324FA8890A7BB22EE31F")

$ctx.Load($myContentType)
$ctx.ExecuteQuery()

#Modify this line with the Title for your new Document Set
[
Microsoft.SharePoint.Client.DocumentSet.DocumentSet]::Create($ctx, $parentFolder, "Whatever document set name you want goes here", $myContentType.Id)

$ctx.ExecuteQuery()


by Paul Choquette via Everyone's Blog Posts - SharePoint Community

No comments:

Post a Comment