PowerShell 2.0 Adding Columns to Document Libries in a Site Collection.

Adding Columns to a SharePoint List across an entire Site Collection should be done like this using PowerShell (with Power GUI):
 
Note that the use of the following works with all the defaults... 
$fieldDateTime = [Microsoft.SharePoint.SPFieldType]::DateTime
$list.Fields.Add("Compliance Date",$fieldDateTime,$false) 

However, you get MORE Control over Field Attributes using this method:
$fldXml = "<Field Type='DateTime' DisplayName='Report Sign Off Date' Format='DateOnly' Name='Report Sign Off Date'/>"     
$list.Fields.AddFieldAsXml($fldXml) 
 
Both methods are used in the code block below. 
if ((Get-PSSnapin | ? { $_.Name -eq "Microsoft.SharePoint.PowerShell" }) -eq $null) {
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}
[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges( 
{
 $site =  Get-SPSite -Identity "https://mysite.com/sites/thissite"
 Write-Host "Script Started"
 [array]$ListNames = "ListA", "ListB"
 $NumberofReportsinRecordFound = $false
 $ReportSignOffDateFound = $false
 $ComplianceDateFound = $false
 foreach($web in $site.AllWebs)
 {
  #Get number if Lists rather than using foreach as we will be modifying the List
  
  for($i=0; $i -lt $web.Lists.Count; $i++)
  {
   $list = $web.Lists[$i]
   if($list.BaseType -eq "DocumentLibrary" -and $list.BaseTemplate -notmatch "Catalog")
   {
    Write-Host $web.Title
    Write-Host $list
        
    $NumberofReportsinRecordFound = $false
    $ReportSignOffDateFound = $false
    $ComplianceDateFound = $false
      
    if($ListNames -contains $list.Title)
    {
     for($j=0; $j -lt $list.Fields.Count; $j++)
     {
      $field = $list.Fields[$j]
      if($field.Title -like "*do not use*")
      {
          Write-Host "do not use deleted!"
       $list.Fields.Delete($field)
       $list.Update() 
      }
      
      if($field.Title -like "Number of Reports in Record")
      {
       $NumberofReportsinRecordFound = $true
      }
      if($field.Title -like "Report Sign Off Date")
      {
       $ReportSignOffDateFound = $true
      }
      
      if($field.Title -like "Compliance Date")
      {
       $ComplianceDateFound = $true
      }
     }
        $fieldNumber = [Microsoft.SharePoint.SPFieldType]::Number

     if(!$NumberofReportsinRecordFound){
      $list.Fields.Add("Number of Reports in Record",$fieldNumber,$false)
      $fld = $list.Fields["Number of Reports in Record"]
     }
     
     if(!$ReportSignOffDateFound){
      $fldXml = "<Field Type='DateTime' DisplayName='Report Sign Off Date' Format='DateOnly' Name='Report Sign Off Date'/>"
      $list.Fields.AddFieldAsXml($fldXml)
     }
     
     if(!$ComplianceDateFound){
      $fldXml = "<Field Type='DateTime' DisplayName='Compliance Date' Format='DateOnly' Name='Compliance Date'/>"
      $list.Fields.AddFieldAsXml($fldXml)
     }
     $list.Description = "Inspection Reports"
     $list.Update()
    }
   }
  }
 }
 $web.Dispose()
 $site.Dispose()
 Write-Output "Script Ended"
}
)

Add comment

Loading