Article
6 comments

Hide fields from list forms using PowerShell

Since SharePoint 2010 was introduced there are much more ways to customize forms for list and document libraries. In most cases there is only the simple requirement to hide fields or have better control over them on lists and content types. SharePoint 2010 is the same way limited for hiding fields like it was in MOSS 2007. For advance control it was always necessary to code those fields. So first let’s take a look at how you can controls fields on the list using the normal settings dialog. The base setup in this test is a custom list called “List” with a new column called “NewColumn” which is of type single line of text.

There are no settings available where you can control where the fields should be appearing. The options to hide a field form certain forms are even not available in SharePoint. So the only way to remove the fields from NewFormDialog for example will be to set up a custom list form. No not at this stage.

When the management of content type is enabled to the list using some advance settings will come up to the fields. This can be done in the list settings:

  • “Advanced settings”
  • “Allow management of content types”
  • “Yes”

Behind ever list and library there is a content type assigned. For my custom lists this is the “Item” content type. After selecting this content type there is the “NewColumn” I created available too. On the column the following view options can be selected.

Required optional or hidden is good but won’t help in most cases. So what now? Create a custom list form? No, this is not needed.

SharePoint developer might find SPField Object from the server object model. So if a developer creates a custom content type the following options can be selected in CAML Definitions or as properties using object model.

  • ShowInDisplayForm
  • ShowInEditForm
  • ShowInListSettings
  • ShowInNewForm
  • ShowInVersionHistory
  • ShowInViewForms

I think the names of those properties are self-explaining. Is there coding needed to use those properties? Yes it is but not as a WSP. This field can set using PowerShell the following example demonstrates how to set those properties by a simple script which only opens the list using object model and sets the values, which are basically Boolean values. I use simply 0 and 1 values to set.

0         enables this property

1         disable this property

So for hiding our column in the new form dialog we just simply need to set the value of ShowInNewForm to false or 0.

# First load SharePoint Core Assembly
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)

$url = "http://myserver:Port";
$list = "List";
$fieldname = "NewColumn";

#Setting up context
$contextSite = New-Object Microsoft.SharePoint.SPSite($url);
$contextWeb = $contextSite.OpenWeb();

$list = $contextWeb.Lists.TryGetList($list);
$field = $list.Fields[$fieldname];

# Controls Field in Edit Form
$field.ShowInEditForm = 1;
# Controls Field in New Form
$field.ShowInNewForm = 0;
# Controls Field in New Form
$field.ShowInDisplayForm = 1;
# Hides fields from list settings
$field.ShowInListSettings = 1;
# Hides fields from version history
$field.ShowInVersionHistory = 1;
# Hides fields form selection in views
$field.ShowInViewForms = 1;
# Don't forget to update this field
$field.Update();
# And finally dispose everything.
$contextWeb.Dispose();
$contextSite.Dispose();

So how does it look after executing the script:

NewColumn Hidden

NewColumn is hidden on NewForm but still avaliable on EditForm.

Conclusion:

I actually can’t say what the reason is behind those properties are available using object model but not be available using the interface or SharePoint Designer. Those properties will give power user better control over their fields and will not require custom list forms using SharePoint Designer or InfoPath Forms for every simple field hiding. Those properties are not new they were still available in Moss 2007. Might this will be supported by SharePoint 2013 or 2014.

6 Comments

  1. very nice post!!!

    I hope indeed that newer version of MOSS will also make those properties opn through web interface.

    Reply

  2. I have been looking all over for a way to do this but it needs to be done when the user clicks the item. I cannot use the ShowInDisplayForm property programmtically on page load so I decided to use the FieldControlCollection and use the ControlMode on each base field control to set if it should be used in display mode when editing as well as hiding fields using the Visible property. I get some MS javascript errors when I hide rich text editor fields but mainly I cannot save when I use the ControlMode to make fields read only on Edit form.

    Reply

    • For the rich text field you have two fields that needs to be set with ControlMode Display. I tried your solution with SharePoint Designer and it works well and the items can be saved. So what i did was only to change the ControlMode from Edit to Display:
      [code]
      <SharePoint:FormField runat="server" id="ff2{$Pos}" ControlMode="Edit" FieldName="Body" _designer:bind="{ddwrt:DataBind(‘u’,concat(‘ff2′,$Pos),’Value’,’ ValueChanged’, ‘ID’,ddwrt:EscapeDelims(string(@ID)),’@Body’)}"/>
      <SharePoint:FieldDescription runat="server" id="ff2description{$Pos}" FieldName="Body" ControlMode="Edit"/>[/code]
      To:
      [code]
      <SharePoint:FormField runat="server" id="ff2{$Pos}" ControlMode="Display" FieldName="Body" _designer:bind="{ddwrt:DataBind(‘u’,concat(‘ff2’,$Pos), ‘Value’, ‘ValueChanged’, ‘ID’, ddwrt:EscapeDelims(string(@ID)),’@Body’)}"/>
      <SharePoint:FieldDescription runat="server" id="ff2description{$Pos}" FieldName="Body" ControlMode="Display"/>
      [/code]
      I won’t get any javascript error.

      Reply

  3. It seems that most of the columns are sealed. So at first you have to unseal them, update them and then seal them back.

    Reply

  4. This is exactly what I am looking, could you please let me know how can I hide multiple columns for a list in edit and all fields in new form using powershell?

    Reply

    • You just need to execute the script for every field you like to hide or iterate in a loop over all those field.

      Reply

Leave a Reply

Required fields are marked *.


This site uses Akismet to reduce spam. Learn how your comment data is processed.