Add a column to a List View in SharePoint Site Collection

Introduction

In this blog we will about to add a column to a specific view in a specific list in multiple webs in SharePoint 2013 using PowerShell method. The list exists in every sub web. The view exists in every list. There are hundreds of sub webs that I need to traverse through and it will take time.

So for that I have created a small PowerShell script to work on this.

Steps

  1. Start your windows PowerShell on your computer.
  2. Right click and select Run as administrator option.
  3. Paste the below script on the PowerShell window and click the enter button.
  4. Check your SharePoint site page will be created successfully.
  1. # -------------------------------------------------------------------------    
  2. # PURPOSE: ADD A COLUMN TO A SPECIFIED VIEW IN A SPECIFIED LIST IN MULTIPLE WEBS WITHIN A SITE COLLECTION.    
  3. # PARAMETERS: 1. SITE COLLECTION URL; 2. COLUMNNAME; 3. LISTNAME; 4. VIEWNAME    
  4. # -------------------------------------------------------------------------    
  5.     
  6. function Add-ColumnToView    
  7. {    
  8. Param(    
  9. [Parameter(Mandatory=$true)]    
  10. [string]$Url,    
  11. [Parameter(Mandatory=$true)]    
  12. [string]$ColumnName,    
  13. [Parameter(Mandatory=$true)]    
  14. [string]$ListName,    
  15. [Parameter(Mandatory=$true)]    
  16. [string]$ViewName    
  17. ) # END PARAMS    
  18.     
  19. if ($Url)    
  20. {    
  21.     $site = Get-SPSite $Url    
  22.     
  23.     if ($site)    
  24.     {    
  25.         # GET THE SUB WEBS IN SITE COLLECTION       
  26.         $allWebs = $site.allwebs    
  27.   
  28.         # LOOP THROUGH EACH SUB WEB    
  29.         foreach ($spweb in $allWebs)     
  30.         {       
  31.             try    
  32.             {    
  33.                 # FIND THE LIST MATCHING THE $LISTNAME    
  34.                 $list = $spweb.GetList($spweb.Url + "/Lists/" + $ListName)    
  35.   
  36.                 # CHECK IF THE LIST OBJECT EXISTS    
  37.                 if ($list)    
  38.                 {    
  39.                     # GET THE SPECIFIC VIEW    
  40.                     $view = $list.Views[$ViewName]    
  41.   
  42.                     # CHECK IF THE VIEW OBJECT EXISTS    
  43.                     if($view)     
  44.                     {    
  45.                         # DELETE IF ALREADY EXIST    
  46.                         while($view.ViewFields.ToStringCollection().Contains($ColumnName))    
  47.                         {    
  48.                             write-host (" Deleting Column From: " + $spweb.Url) -foregroundcolor yellow    
  49.                             $view.ViewFields.delete($ColumnName)    
  50.                             $view.Update()    
  51.                         }    
  52.   
  53.                         # ADD COLUMN    
  54.                         if(!$view.ViewFields.ToStringCollection().Contains($ColumnName))    
  55.                         {       
  56.                             write-host (" Adding Column To: " + $spweb.Url) -foregroundcolor green    
  57.                             $view.ViewFields.add($ColumnName)    
  58.                             $view.Update()    
  59.                         }    
  60.                     }    
  61.                 }    
  62.             }       
  63.             catch [Exception]    
  64.             {    
  65.                 write-host ("  Error: " + $_.Exception.ToString()) -foregroundcolor red    
  66.             }    
  67.         }    
  68.     }    
  69.     else    
  70.     {    
  71.         write-host ("  Error: " + "Site Collection Not available") -foregroundcolor white    
  72.     }    
  73.     $site.Dispose()    
  74. }    
  75. else    
  76. {    
  77.     write-host ("  Error: " + "Site Collection URL Not Specified.") -foregroundcolor white    
  78. }    
  79. } #END FUNCTION   

Run the script with the required administrator privilege.

To verify that, go the Pages List in particular and make sure that available or not.