none
Programmatically Hiding a Content Type Field RRS feed

  • Question


  • Here is my scenario,
    I have 2 content types (CT1 and CT2). CT2 is created programmatically using PowerShell by the lines of code given below,
    $CT1= $spWeb.ContentTypes["My Content Type 1"]
    
    $CT2 = $spWeb.ContentTypes["My Content Type 2"]
    
    if($CT2 -eq $null)
    {
      $CT2 =New-Object Microsoft.SharePoint.SPContentType -ArgumentList @($CT1, $spWeb.ContentTypes, "My Content Type 2");
      $CT2.Group = "My Group";
      $CT2.Description = "Some Description for CT2."; 
      $spWeb.ContentTypes.Add($CT2);
     }
    
    I want to hide some of the columns that I inherited from CT1 in CT2. When I use the below code for hiding columns, I get 'Exception setting "Hidden": "Cannot change Hidden attribute for this field"'
    <pre lang="x-powershell">$field = $CT2.Fields["MyField"] #MyField - I want to hide this field
    	
    $field.Hidden = $true;
    $field.Update()
    
    After I looked deeper into the problem, I found that, CanToggleHidden property for the field is set to 'False'. Hence, I decided to change the flag using reflection by using the lines below,
    $field = $CT2.Fields["MyField"] #MyField - I want to hide this field
    	
    $bindingFlags = [Reflection.BindingFlags] "NonPublic,Instance"
    [System.Type] $type = $field.GetType();
    [Reflection.MethodInfo] $mdInfo = $type.GetMethod("SetFieldBoolValue",$bindingFlags);
    
    $object = [System.Object] @("CanToggleHidden",$true);
    $mdInfo.Invoke($field,$object);
    
    $field.Hidden = $true;
    $field.Update()
    
    This time, I did not get error at $field.Hidden but at $field.Update() method call, I get 'Exception calling "Update" with "0" argument(s): "This functionality is unavailable for fields not associated with a list."'
    Suprisingly from UI (Site Settings -> Site Content Types), I can browse to 'CT2' content type and hide columns that I don't want!!!!
    My questions is, Is there a way to hide unwanted columns programmatically?

    SSK
    Thursday, July 14, 2011 8:37 AM

Answers

  • Thanks for the help Pavel. For the benefit of community users, below is the final snippet for hiding columns from inherited content type using PowerShell

     

    $field = $CT2.FieldsLinks["MyFieldInternalName"] #MyFieldInternalName- I want to hide this field
    	
    $field.Hidden = $true
    
    $CT2.Update($true)
    

    • Marked as answer by ShivaKumarS Thursday, July 14, 2011 3:03 PM
    Thursday, July 14, 2011 3:03 PM
  • You should reference the field you want to hide not from the Fields collection, but from the FieldLinks one.

    Try to change powershell string:

    $field = $CT2.Fields["MyField"] 
    

    to this one:

    $field = $CT2.FieldLinks["MyField"] 
    

    After it, the script should work without exceptions.

    • Marked as answer by ShivaKumarS Thursday, July 14, 2011 3:03 PM
    Thursday, July 14, 2011 10:20 AM

All replies

  • You should reference the field you want to hide not from the Fields collection, but from the FieldLinks one.

    Try to change powershell string:

    $field = $CT2.Fields["MyField"] 
    

    to this one:

    $field = $CT2.FieldLinks["MyField"] 
    

    After it, the script should work without exceptions.

    • Marked as answer by ShivaKumarS Thursday, July 14, 2011 3:03 PM
    Thursday, July 14, 2011 10:20 AM
  • Thanks for the help Pavel. For the benefit of community users, below is the final snippet for hiding columns from inherited content type using PowerShell

     

    $field = $CT2.FieldsLinks["MyFieldInternalName"] #MyFieldInternalName- I want to hide this field
    	
    $field.Hidden = $true
    
    $CT2.Update($true)
    

    • Marked as answer by ShivaKumarS Thursday, July 14, 2011 3:03 PM
    Thursday, July 14, 2011 3:03 PM
  • the above code  helped me setting the hidden  site column [  in content type hub  ] to  visible = false in my subscriber site collections

    Das

    Wednesday, March 25, 2015 11:49 AM