PowerShell CSOM – 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.

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

https://github.com/msisgreat/ps/blob/master/HideColumn.ps1

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

$username = "username@domain.com"
$password = ""
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
Write-Host "Connecting to site ..."
$srcUrl = "" ## https://sitename/sites/sitename
$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

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: