hide a list field from the create forum

john john 1,021 Reputation points
2020-10-09T17:05:22.81+00:00

I have a field which was created at the list level inside my online SharePoint site, and i want to hide it from the create form, so i run the following PnP code:-

PS C:\WINDOWS\system32> Connect-PnPOnline -Url https://****.sharepoint.com/sites/***
PS C:\WINDOWS\system32> $fieldTitle = "ClosureSummary"
PS C:\WINDOWS\system32> $list2 = Get-PnPList -Identity Lists/ProjectUpdateSystem
PS C:\WINDOWS\system32> $customfield = $list2.Fields.GetByInternalNameOrTitle($fieldTitle)
PS C:\WINDOWS\system32> $customfield.SetShowInNewForm($false)
PS C:\WINDOWS\system32> $Context.ExecuteQuery()
PS C:\WINDOWS\system32> $customfield.UpdateAndPushChanges($true)
PS C:\WINDOWS\system32> $Context.ExecuteQuery()

Now i did not get any error but the field is still showing inside the list create form, any advice?

Microsoft 365 and Office | SharePoint | For business | Windows
{count} votes

2 answers

Sort by: Most helpful
  1. Sharath Kumar Aluri 3,071 Reputation points
    2020-10-09T18:07:11.137+00:00

    Try with the below sample script for your requirement:

    #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"
    
    Function Get-SPOContext([string]$Url,[string]$UserName,$Password)
    {
        write-host Get-SPOContext   
        $Context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
        $Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $Password) #SecurePassword
        return $Context
    }
    
    $UserName = "******@globalsp.onmicrosoft.com"
    $Password = Read-Host -Prompt "Password" -AsSecureString    
    $Url = "https://globalsp.sharepoint.com/sites/Contoso"
    $listTitle = "ProjectUpdateSystem"
    $FieldName = "ClosureSummary"
    
    $Context = Get-SPOContext -Url $Url -UserName $UserName -Password $Password
    $List = $Context.Web.Lists.GetByTitle($ListTitle)
    $Context.Load($List)
    $Context.ExecuteQuery()
    
    #Retrieve field
    $field = $List.Fields.GetByInternalNameOrTitle($FieldName)
    $Context.Load($field)
    $Context.ExecuteQuery()
    $field.SetShowInNewForm($false)
    $field.UpdateAndPushChanges($true)
    
    $Context.ExecuteQuery()
    
    Write-Host Completed
    

    Thanks & Regards,

    0 comments No comments

  2. Allen Xu_MSFT 13,866 Reputation points
    2020-10-12T07:12:41.85+00:00

    The following code can meet your needs, you can refer to it:

    Connect-PnPOnline <tenant-site-url>  
      
    $ctx = Get-PnPContext  
    $field = Get-PnPField -Identity <field-name> -List <list-name>  
      
    $field.SetShowInNewForm($false)  
    $field.Update()  
    $ctx.ExecuteQuery()  
    

    Note: Please update the <tenant-site-url>, <field-name> and <list-name> with your SharePoint Online environment.

    I hope this information has been useful, please let me know if you still need assistance.


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.