SharePoint Online: How to Hide a Column in Content Type using PowerShell

Requirement: Hide a Column from Content Type in SharePoint Online.

How to Hide a Column using Content types in SharePoint Online?
Columns can be hidden at both content type level or list level in SharePoint. You can hide a column from appearing in list forms, such as: NewForm.aspx, EditForm.aspx, DispForm.asxp using content type. Enable the content type first by going to:
  • Go to List Settings >> Advanced Settings >> Enable "Allow management of content types"  check the box
Once content types are enabled, you can hide any field of that content type.
  • Go to List Settings again >> Under Content Types Click on Item content type (or whatever content type applicable) 
  • Click on Title column (or the column you wish to hide) under columns group >> Click on Hidden option.
    SharePoint Online: How to Hide a Column in Content Type using PowerShell
Now the specific column won't appear on any of the forms as listed above. You can also hide the column from "Views" too by removing them from list views.

PowerShell to Hide a Column from List Content Type in SharePoint Online:
#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 to Hide Column From List Content Type in SharePoint Online
Function Hide-ColumnFromContentType()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ContentTypeName,
        [Parameter(Mandatory=$true)] [string] $ColumnName
    )

    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()

        #Get the content type
        $ContentTypes = $List.ContentTypes
        $Ctx.Load($ContentTypes)
        $Ctx.ExecuteQuery()
        $ContentType = $ContentTypes | Where {$_.Name -eq $ContentTypeName}

        #Get the Field link from content type
        $FieldLinks =$ContentType.FieldLinks
        $Ctx.Load($FieldLinks)
        $Ctx.ExecuteQuery()
        $FieldLink = $FieldLinks | Where {$_.Name -eq $ColumnName}

        #Hide the Field link
        $FieldLink.Hidden = $True
        
        #Update Content type
        $ContentType.update($False)
        $Ctx.ExecuteQuery()

        Write-host "Column Hidden from the Content Type Successfully!" -ForegroundColor Green  
    }
    Catch {
        write-host -f Red "Error Hiding Column in Content Type!" $_.Exception.Message
    } 
} 

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$ContentTypeName="Item"
$ColumnName="Priority"

#Call the function 
Hide-ColumnFromContentType -SiteURL $SiteURL -ListName $ListName -ContentTypeName $ContentTypeName -ColumnName $ColumnName
Setting the field "Hidden" hides it from all list forms. Instead, you can hide the column from specific forms such as display form with:
 $FieldLink.ShowInDisplayForm = $false 

SharePoint Online: Hide a Column in List Content Type using PnP PowerShell
Assuming content type is already added to the given list, here is the PowerShell to hide a column from content type as list level.
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$ListName ="Team Documents"
$ContentTypeName ="Crescent Invoice Template V2"
$FieldName = "ProjectManager"

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

#Get the Client Context
$Context = Get-PnPContext
 
#Get the content type from List
$ContentType = Get-PnPContentType -Identity $ContentTypeName -List $ListName

If($ContentType)
{
    #Get the Field from Content Type
    $ContentTypeField = $ContentType.Fields.GetByInternalNameOrTitle($FieldName)
    $Context.Load($ContentTypeField)
    $Context.ExecuteQuery()
 
    #Set Hidden Property of the field
    $ContentType.FieldLinks.GetById($ContentTypeField.Id).Hidden = $True
    $ContentType.Update($False) #Update children
    $Context.ExecuteQuery()
    Write-host -f Green "Field '$($ContentTypeField.Title)' Set to Hidden!"
}
sharepoint online hidden field in content type
Similarly, You can set a column in Content type as mandatory by turning on "Required" Property. E.g.
$ContentType.FieldLinks.GetById($ContentTypeField.Id).Required = $True

PnP PowerShell to Hide a Field at Content Type Level:
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$ContentTypeName ="Crescent Project V2"
$FieldName = "ProjectManager" #Internal Name

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

#Get the Client Context
$Context = Get-PnPContext
 
#Get the content type
$ContentType = Get-PnPContentType -Identity $ContentTypeName -ErrorAction Stop
 
#Get the Field from Content Type
$ContentTypeField = $ContentType.Fields.GetByInternalNameOrTitle($FieldName)
$Context.Load($ContentTypeField)
$Context.ExecuteQuery()
 
#Set Hidden Property of the field
$ContentType.FieldLinks.GetById($ContentTypeField.Id).Hidden = $True
$ContentType.Update($True) #Update Children 
$Context.ExecuteQuery()
Write-host -f Green "Field '$($ContentTypeField.Title)' Set to Hidden!"
SharePoint Online: How to Hide a Column in Content Type using PowerShell SharePoint Online: How to Hide a Column in Content Type using PowerShell Reviewed by Salaudeen Rajack on January 15, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.