Share via


SharePoint 2010 Hide List Field Columns Using PowerShell


SharePoint 2010 Hide List Field Columns Using PowerShell


Requirement

We have added three Managed Metadata Column in a SharePoint List and they are named as below: Themes 1 Themes 2 Themes 3. Due to structural change in the organization we need to remove this from the LIST but contents need to be available in a search result.

Summary

The requirement is clear but removing the managed metadata field is not a good option. We can hide it from the list so that the search crawler will not contain old crawled records. Remember: only existing metadata property can be searched because end users can't update the hidden field in future.

Explore List

Before we begin it's best practice to explore the list and see the settings. Using PowerShell is the best option and SharePoint 2010 Manager [GUI] is also a good tool. The reason behind this is to avoid risks. When we see the requirement sheet it has a display name but not the internal name of the fields. So play safe it in the production environment.

Code

#region Verify the Internal Name - CONFIRM BEFORE COMMITING CHANGE
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$web = Get-SPWeb -Identity 'http://www.site.com/subsite'
$List = $web.Lists['PowerShell']
$Fields = $list.Fields.GetFieldByInternalName('Age')
$Field | Select InternalName
#endregion

#region Check the Settings - Needs to be Tru if not set to TRUE
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$web = Get-SPWeb -Identity 'http://www.site.com/subsite'
$List = $web.Lists['PowerShell']
$Fields = $list.Fields.GetFieldByInternalName('Age')
$Field.CanToggleHidden
#endregion

#region Full Code - Commit Change
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$web = Get-SPWeb -Identity 'http://www.site.com/subsite'
$List = $web.Lists['PowerShell']
$Fields = $list.Fields.GetFieldByInternalName('Age')
#$Fields.CanToggleHidden
$Fields.Hidden = $true
$Fields.ShowInDisplayForm = $false
$Fields.ShowInViewForms = $false
$Fields.ShowInEditForm = $false
$Fields.ShowInListSettings = $false
$Fields.Update()
#endregion

#region = This is to hide from All Items View [Default]
$view = $List.DefaultView
$view.ViewFields.Delete('Age')
$view.Update()
#endregion

#region = How to revert?
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$web = Get-SPWeb -Identity 'http://www.site.com/subsite'
$List = $web.Lists['PowerShell']
$Fields = $list.Fields.GetFieldByInternalName('Age')
#$Fields.CanToggleHidden
$Fields.Hidden = $false
$Fields.ShowInDisplayForm = $true
$Fields.ShowInViewForms = $true
$Fields.ShowInEditForm = $true
$Fields.ShowInListSettings = $true
$Fields.Update()

$view = $List.DefaultView
$view.ViewFields.Add('Age')
$view.Update()
#endregion

Screenshot