Within a PS script, I'm trying to add a site column and add this column to a custom content type.
Here is my script :
add-pssnapin Microsoft.SharePoint.PowerShell -EA 0
function AddFieldToContentType([Microsoft.SharePoint.SPWeb] $web, [string]$contentTypeName, [string]$xmlSchema)
{
$fieldName = $web.Fields.AddFieldAsXml($xmlSchema)
$web.Update()
$field = $web.Fields[$fieldName]
$ct = $web.AvailableContentTypes[$contentTypeName]
$link = new-object Microsoft.SharePoint.SPFieldLink $field
$ct.FieldLinks.Add($link)
$ct.Update($true)
}
[Microsoft.SharePoint.SPWeb]$web = get-spweb -Identity http://somewhere
[xml]$xml = 'my field schema truncated for readability'
AddFieldToContentType $web "my content type name" $xml
This fails at the line $ct.Update($true)
with this error :
Exception calling "Update" with "1" argument(s): "The collection cannot be modified."
What's wrong with this code ?
Thanks