SharePoint Online: How to Hide Columns from List using PowerShell?

Requirement: SharePoint Online PowerShell to Hide Field
In a project request list, when user fills the form we wanted to hide the "Status" column by default and then have a workflow to populate status of the particular request. Similarly, for Edit column as well, but show it only on View form. So, How to hide column from SharePoint online list?

SharePoint Online Hide Column from list using PowerShell:
Here is the PowerShell script to hide list columns from SharePoint Online list.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/prjects/"
$ListName="ProjectRequest"
$FieldName = "Status" #Display Name

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred
  
    #Get the web, List and Field objects
    $Web=$Ctx.Web
    $List=$web.Lists.GetByTitle($ListName)
    $Field = $List.Fields.GetByTitle($FieldName)

    #Hide the column from New & Edit forms
    $Field.SetShowInEditForm($False) 
    $Field.SetShowInNewForm($False) 
    $Field.UpdateAndPushChanges($True)
    $Ctx.ExecuteQuery()
     
    Write-host -f Green "List Field hidden Successfully!"
}
Catch {
    write-host -f Red "Error hiding List Column: " $_.Exception.Message
}
This PowerShell hides the specific field from New form and Edit forms of the list!
sharepoint online hide column

SharePoint Online: Hide column in new item form using PnP PowerShell
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"
$ListName = "Projects"
$FieldName =  "Status"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Field
$Field = Get-PnPField -List $ListName | Where {$_.Title -eq $FieldName}

#Hide the field from New Form
$Field.SetShowInNewForm($False)
#$Field.SetShowInEditForm($True)
Invoke-PnPQuery
SharePoint Online: How to Hide Columns from List using PowerShell? SharePoint Online: How to Hide Columns from List using PowerShell? Reviewed by Salaudeen Rajack on April 08, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.