January 20, 2020

Hide Field in New and Edit Forms of SharePoint Online List using Powershell

Hello,

Just tried to hide SharePoint List fields from New and Edit Forms,  wrote a powershell script,

  1. Connect to SharePoint
  2. Load SharePoint List context,
  3. Get Field to hide from forms 
  4. Use functions to hide field from SharePoint forms.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
#Config Parameters
$SiteURL= "SharePoint Site URL"
$ListName="ListName"
$FieldName = "ColumnName" #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
    $Ctx.Load($Web);
    $Ctx.ExecuteQuery()

    $List=$web.Lists.GetByTitle($ListName)
    $Ctx.Load($List);
    $Ctx.ExecuteQuery()
    

    $Field = $List.Fields.GetByTitle($FieldName)

    $Ctx.Load($Field);
    $Ctx.ExecuteQuery()

    #Hide the column from New & Edit forms
    $Field.SetShowInEditForm($False)
    $Ctx.ExecuteQuery()
    $Field.SetShowInNewForm($False)
    $Ctx.ExecuteQuery()
    $Field.UpdateAndPushChanges($True)
    $Ctx.ExecuteQuery()
      
    Write-host -f Green "List Field hidden Successfully!"
}
Catch {
    write-host -f Red "Error hiding List Column: " $_.Exception.Message
}

No comments: