Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

Add Column to View in SharePoint using PowerShell

$
0
0
How to add a column to SharePoint list view using PowerShell?

PowerShell Script to add a field to View:
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
}


Viewing all articles
Browse latest Browse all 1058

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>