1

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()
}
  • 2
    are you sure that $contentType.FieldLinks["KPI_x0020_Type"] exists? – ReTech Jul 12 '13 at 9:37
  • Yes, all I'm doing is refactoring to tidy up the code. As I said, it works in the original script but when I assign $lib.ContentTypes["KPI"] to $contentType in the second script example it fails. – David Clarke Jul 20 '13 at 23:16
1

Your script is breaking in the $contentType line. When you call out the hash table index like that the variable becomes flat like a regular text string. Try this:

$web = Get-SPWeb $url   
$lib = $web.Lists[$libName]
$contentType = $lib.ContentTypes
if ($contentType["KPI"]) {
    $contentType["KPI"].FieldLinks["KPI_x0020_Type"].Hidden = $true
    $contentType.Update()
}

By not calling out the hash table key you preserve the hash table nature going into your if statement. That should get you past the null error. Then as ReTech mentioned, you have to make sure $contentType["KPI"].FieldLinks["KPI_x0020_Type"] exists and is editable/settable. Which should be the case since your first version was working correctly. Hope this helps.

  • Why is it "flattening" it, is this default behaviour in PowerShell? Can you point me at some documentation for this? – David Clarke Jul 28 '13 at 21:09
  • 1
    When you create the $contentType variable, it contains a hash table like object with a lot of information in it. In the ISE console, type in the first three lines of my script then type in $contentType and hit enter. PowerShell will display the hash table. You can use hash table index names to call out one reference in that object such as $contentType["KPI"]. KPI and FieldTypes are both indexes in the hash table. Setting the variable that contains the hash table to only one of its entries removes all the other information in the table. Look at Get-Help about_variables in PowerShell. – MonkeyWrench Jul 29 '13 at 17:33
  • This is a good explaination of hash tables technet.microsoft.com/en-us/library/ee692803.aspx – MonkeyWrench Jul 29 '13 at 17:34
0

Thanks for feedback, it ultimately led to the correct answer below, which was to tell PowerShell the type of the variable used to contain the SPContentType.

$web = Get-SPWeb $url   
$lib = $web.Lists[$libName]
[Microsoft.SharePoint.SPContentType]$contentType = $lib.ContentTypes["KPI"]
if ($contentType) {
    $contentType.FieldLinks["KPI_x0020_Type"].Hidden = $true # <-- no longer throws here
    $contentType.Update()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.