Add Column to View in SharePoint using PowerShell
How to add a column to view in SharePoint?
You can add columns to your view in just a few easy steps. To add a column in SharePoint view, follow these steps:
- Browse to your list or library in SharePoint >> Click on the List or Library tab from ribbon.
- Select the appropriate view from the “Current View” drop-down. Default View gets listed in the current view usually. Click on the “Modify View” button under the “Manage Views” group.
- On the Edit View page, Select the Columns you wish to appear in the particular View.
- Click OK to save. This adds column to list view.
PowerShell to Add a Field to View:
How to add a column to SharePoint list view using PowerShell? Let’s programmatically add a custom column to view using PowerShell.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#configuration parameters
$WebURL="https://portal.crescent.com/projects"
$ListName="Project Milestones"
$ViewName="All Items"
$FieldInternalName="ProjectDescription"
Function Add-FieldToView([Microsoft.SharePoint.SPList]$List, [String]$ViewName, [string]$FieldInternalName)
{
#Get the view
$View = $List.Views[$ViewName]
#To Get the Default View: List.DefaultView
if($view -eq $Null) {write-host "View doesn't exists!" -f Red; return}
#Check if view has the specific field already!
if(!$view.ViewFields.ToStringCollection().Contains($FieldInternalName))
{
$View.ViewFields.Add($FieldInternalName)
#To Delete a field from view: $View.ViewFields.delete($FieldInternalName)
$View.Update()
write-host "Field added to View!" -f Green
}
else
{
write-host "Field Already Exists in the view!" -f Red
}
}
#Get the Web and List
$Web= Get-SPWeb $WebURL
$List = $web.Lists.TryGetList($ListName)
#If List Exists
if ($List )
{
#Call the function
Add-FieldToView $List $ViewName $FieldInternalName
}
SharePoint PowerShell to add a column to default view:
To add column to default view, in the above script, instead of:
$View = $List.Views[$ViewName]
Use:
$View = $List.DefaultView
All Column to All Views in the List using PowerShell:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Function Add-FieldToAllViews([Microsoft.SharePoint.SPList]$List, [string]$FieldInternalName)
{
#Iterate through each view from the list
For($i = $List.Views.Count-1; $i -ge 0; $i--)
{
$View = $List.Views[$i]
#Check if view has the specific field already!
if(!$view.ViewFields.ToStringCollection().Contains($FieldInternalName))
{
$View.ViewFields.Add($FieldInternalName)
$View.Update()
write-host "Field added to View:"$View.Title -f Green
}
else
{
write-host "Field Already Exists in the view:"$View.Title -f Yellow
}
}
}
#Parameters
$WebURL="https://intranet.crescent.com/"
$ListName="Documents"
$FieldInternalName="OverallProgress"
#Get the Web and List
$Web= Get-SPWeb $WebURL
$List = $web.Lists.TryGetList($ListName)
#If List Exists
if ($List )
{
#Call the function to add field to all list views
Add-FieldToAllViews $List $FieldInternalName
}