I need to create a folder in a document library (SPO) via PowerShell/SPO.
The code below creates the folder, but I can't figure out how to set the content type of the folder I just created. I created a content type based on Folder, and it works when I use it through the UI.
When I run the code below, it creates the folder then gets "Unable to index into an object of type Microsoft.SharePoint.Client.Folder" when trying to set $Folder["ContentTypeId"]. What do I need to do to be able to set this?
$SiteURL = "https://mydomain.sharepoint.com/sites/Accounting"
$LibraryName = "AR Files"
$FolderName = "Test"
$ContentType = "AR Folder"
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
# Find the ID of the content type
$Ctx.Load($Ctx.Web)
$Ctx.Load($Ctx.Web.ContentTypes)
$Ctx.ExecuteQuery()
foreach($cc in $Ctx.Web.ContentTypes)
{
if ($cc.Name -eq $ContentType)
{
$CTID = $cc.ID
}
}
# Create folder
$fullname = $LibraryName + "/" + $FolderName
$Folder = $Ctx.Web.Folders.Add($fullname)
$Ctx.ExecuteQuery()
# Load the folder just created
$Ctx.Load($Folder)
$Ctx.ExecuteQuery()
# Set the content type
$Folder["ContentTypeId"] = $CTID
$Folder.Update()
$Ctx.ExecuteQuery()