Friday, January 15, 2016

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()
        [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)

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

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

        #Hide the Field link
        $FieldLink.Hidden = $True
        #Update Content type

        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

#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 

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...