I'm trying to add an existing content type to my SPO libraries. I have an array of my libraries (ListsToAddTo), and I'm using the following script. It always fails when it get's to the AddExistingContentType - with the error "format-default : The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested."
It could be that I'm not loading and executing in the right order - or it could be that when I'm passing the parameter into the AddExistingContentType, it fails?
foreach ($formlibrary in $ListsToAddTo){ $context.Load($context.Web.Lists) $GetByTitle = $context.Web.Lists.GetByTitle($formlibrary) $GetById = $context.Web.AvailableContentTypes.GetById("0x01010A") $context.Load($GetByTitle) $context.Load($GetById) $context.ExecuteQuery() $titleName = $GetByTitle.Title Write-Host "Executed Title Query on $titleName" -ForegroundColor Magenta $GetByTitle.ContentTypes.AddExistingContentType($GetById) $GetByTitle.Update() Write-Host "Updated List" $context.ExecuteQuery() Write-Host "Executed Add Query for $titleName" -ForegroundColor Green }
Joe