Friday, July 15, 2016

SharePoint 2013: easily hide and show list form fields using PowerShell

Introduction

There are a few different ways to make list forms display only the columns you want to display and not display the others.  One way is to create custom New, Edit and Display list forms presenting only those fields that you want presented.  This can be accomplished using SharePoint Designer 2013.  Another way is to employ a list field's ShowInDisplayForm, ShowInNewForm and ShowInEditForm properties.  This way can be accomplished using just a few lines of PowerShell script.

The commandlet that we will be using in this posting is primarily Get-SPWeb, which is needed to get an instance of the website object.  Once obtaining an instance of that object, we'll need to drill down to an instance of the appropriate website list object and then an instance of the appropriate field object in that list object.  This posting shows you how.

Procedure 

First, a test list, Assessments, was created having five custom fields in addition to the default Title field.
Clicking on the new item link displays the usual default NewForm, having all the expected fields:
Now, let's consider the case where we want routine users to only fill out the Title and Comments fields when submitting a new assessment request.  You could add comments to the other fields directing users not to populate these other fields with data, but better UI design is to not display them at all when it's unnecessary.  Let's hide the Reviewer, Status, Assessment Date and Priority fields from the list's NewForm, which is the form that will be used by ordinary users.  Now let's get an instance of the website:
$SPWeb = Get-SPWeb "http://[yourwebsite]"
Next, get an instance of the target list in that website:
$SPList = $SPWeb.Lists["Assessments"]
Now, we need to get instances of all fields we want to hide:
$SPField1 = $SPList.Fields["Reviewer"]
$SPField2 = $SPList.Fields["Status"]
$SPField3 = $SPList.Fields["Assessment Date"]
$SPField4 = $SPList.Fields["Priority"]
This enables us to set each field's ShowInNewForm property, like so:
$SPField1.ShowInNewForm  = $false
$SPField2.ShowInNewForm  = $false
$SPField3.ShowInNewForm  = $false
$SPField4.ShowInNewForm  = $false
Though these instances of the fields' ShowInNewForm property have been set, this setting needs to be updated back to the source object, and this is done via each field object's Update method, like so:
$SPField1.Update()
$SPField2.Update()
$SPField3.Update()
$SPField4.Update()
Lastly, we can dispose of the website object instance:
$SPWeb.Dispose()
After making these changes, the list's NewForm look's like this:
These modifications do not affect the list's DisplayForm or EditForm, which still show all fields:
and

To show these fields in the NewForm again, just set their ShowInNewForm properties back to $false.  This method works for all custom columns that you add to the list.  Note that this method does not work for the system generated Created and Modified fields.

References

  • Thanks to Krishana Kumar in this posting for first showing AlsTechTips about this approach.
  • Using Get-Member, you can see many other column/Field parameters you can easily modify via PowerShell
  • This method does not work for hiding the system-generated Created and Modified fields.

No comments: