CSOM – How to hide site columns from edit and new form

In this blog I will explain how to hide some of the columns from the edit and the new form for the list. CSOM has the API to hide column. There are three main api to hide / show the column

  1. SetShowInEditForm
  2. SetShowInNewForm
  3. SetShowInDisplayForm

Consider the below sample scenario where you are developing a Leave Application using custom list. When the user fills in the Leave application you don’t want to show status and comment field. So in the below scenario you want to hide status and comment field from edit form and the new form for the list. But the same fields need to be shown on the view form.

csom-1

 

Run the below script to hide those fields from both the form.

Add-Type -Path “C:\Microsoft.SharePoint.Client.dll”

Add-Type -Path “C:\Microsoft.SharePoint.Client.Runtime.dll”

 

$username = "username@domain.com"

$password = "<password>"

$securePassword = ConvertTo-SecureString $password -AsPlainText -Force

$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)

Write-Host "Connecting to site ..."

$srcUrl = "<url>" ## https://sitename/sites/espc15

$srcLibrary = "Leave Application"

$srcContext = New-Object Microsoft.SharePoint.Client.ClientContext($srcUrl)

$srcContext.Credentials = $credentials

$srcWeb = $srcContext.Web

$srcList = $srcWeb.Lists.GetByTitle($srcLibrary)

$srcContext.Load($srcList)

$srcContext.ExecuteQuery()

Write-Host "Connected successfully"



$fields = @("Application Status","Approver Comment")

foreach($fieldname in $fields)

{

    Write-Host "Hiding from edit & new form: " $fieldname

    $fieldToEdit = $srcList.Fields.GetByTitle($fieldname);

    $srcContext.Load($fieldToEdit)

    $srcContext.ExecuteQuery()

    $fieldToEdit.SetShowInEditForm($false)

    #$fieldToEdit.Update()

    $srcContext.ExecuteQuery()

    $fieldToEdit.SetShowInNewForm($false)

    #$fieldToEdit.Update()

    $srcContext.ExecuteQuery()

    #$srcWeb.Update()



}



 

Sample PowerShell output

csom2

 

 

 

 

 

After the script is successfully executed the fields are hidden in the new and edit form. Below is the screenshot after the fields are hidden.

csom3png

 

 

 

Share this on...

Rate this Post:

Share:

Topics:

General