Wednesday, December 30, 2015

Add Column to View in SharePoint using PowerShell

How to add column to view in SharePoint?
To add 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 current view usually. Click on "Modify View" button under "Manage Views" group. 
  • In Edit View page, Select the Columns you wish to appear in the particular View. 
    sharepoint powershell add column to default 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? Lets programmatically add 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 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="http://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
}



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


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