I have a strange issue when using PowerShell to modify a library ContentType. The following script works correctly:
$web = Get-SPWeb $url
$lib = $web.Lists[$libName]
$lib.ContentTypes["KPI"].FieldLinks["KPI_x0020_Type"].Hidden = $true
$lib.ContentTypes["KPI"].Update()
I want to tidy the code up a little but when I assign the ContentType
to a variable it throws System.Management.Automation.RuntimeException: Cannot index into a null array
:
$web = Get-SPWeb $url
$lib = $web.Lists[$libName]
$contentType = $lib.ContentTypes["KPI"]
if ($contentType) {
$contentType.FieldLinks["KPI_x0020_Type"].Hidden = $true # <-- throws here
$contentType.Update()
}
$lib.ContentTypes["KPI"]
to$contentType
in the second script example it fails.