kwizcom banner advertisement

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?
Well 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.

PowerShell to Hide a Column from 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 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: 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.