July 22, 2013

PowerShell how to add New Filed / Column type number / integer choice / DropDown in SharePoint

"PowerShell  how to add New Filed / Column type number / integer choice / DropDown in SharePoint"

Here is another example for How PowerShell is Powerful I mean useful.

Export List of site/ subsites

 $site = Get-SPSite "http://ServerName/SiteCollection"  
 Set-content ".\urls.csv" "URL"  
  foreach ($web in $site.AllWebs) {  
     write-host "Web URL -->" $web.URL  
     AC ".\urls.csv" $web.URL  
   }   

#above code will a CSV with heading URL and list of sites (like below) with name 'urls.csv'
URL
http://servername/B
http://servername/A

///////////////////////////// Add Number or Integer Column ////////////////////////////

  $csv = Import-CSV ".\urls.csv"  
  $spFieldType = [Microsoft.SharePoint.SPFieldType]::Number  
  $spFieldName = "Worked Hours"     
 foreach ($siteURL in $csv) {  
   # Web URL  
   $web = Get-SPWeb -Identity $siteURL.url;  
   # SPList name  
    $list = $web.Lists["Tasks"]  
   if($list -ne $null)  
   {  
     write-host "`nList is" $list "on Site " $web.Title " " $web.url;  
     $YN= Read-host "Press Enter to Confirm or Press ctrl+c"  
     if($YN -eq $null)  
     {  
      write-host "Checking Existing Columns."  
     $TF=$true;  
     ForEach ($id in $fld)  
     {  
       if($id -match $spFieldName)  
       {  
         Write-host "Column already exist`n" -foregroundcolor yellow;  
         $TF=$false;  
       }        
     }  
       if($TF)  
       {  
      Write-host "......Creating Column.....";      
      #The new field is not required Filed, so the value of the third parameter is 0  
      $list.Fields.Add($spFieldName, $spFieldType, 0)  
      $list.Update()  
      Write-host "Adding Decimal two places formate"  
      $spField = $list.Fields.GetField($spFieldName)  
      $spField.DisplayFormat = "2"    
      $spField.Update()  
      Write-host " **Item Added** `n" -foregroundcolor green  
      }  
      }  
   }  
   else{Write-Host "No List Found"}  
 }  

/////////////////////////////////// Add DropDown or Choice Column ////////////////////////////////////

  $spFieldName = "My Column"  
 $Choices = New-Object System.Collections.Specialized.StringCollection  
 $Choices.Add("Fixed")  
 $Choices.Add("Not Fixed")  
 $csv = Import-CSV ".\urls.csv"  
 foreach ($siteURL in $csv) {  
   # Web URL  
   $web = Get-SPWeb -Identity $siteURL.url;  
   # SPList name  
    $list = $web.Lists["Projects"]  
   if($list -ne $null)  
   {  
     write-host "`nList is" $list "on Site " $web.Title " " $web.url;  
     $YN= Read-host "To Confirm Press Enter......."  
     if($YN -eq '')  
     {  
     #column of type 'Choice Column'  
     $fld  = $list.Fields  
     write-host "Checking Existing Columns."  
     $TF=$true;  
     ForEach ($id in $fld)  
     {  
       if($id -match $spFieldName)  
       {  
         Write-host "Column already exist`n" -foregroundcolor yellow;  
         $TF=$false;  
       }        
     }  
       if($TF)  
       {  
         Write-host "......Creating Column.....";  
         #Add Choice Field to list  
         $list.Fields.Add($spFieldName,[Microsoft.SharePoint.SPFieldType]::Choice,$FALSE,$FALSE,$Choices)  
          $list.Update()  
          Write-host " **Item Added** `n" -foregroundcolor green  
          $views = $list.Views["All Items"];  
          $views.ViewFields.Add($spFieldName);  
          $views.Update()  
        }  
     }  
   }  
   else{Write-Host "No List Found"}  
 }  

--
Regards,
Praveen Pandit


No comments:

Post a Comment