3

I need to add five new columns to existing SharePoint 2010 libraries using powershell.

I am using below script:

$ver = $host | select version 
if($Ver.version.major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"} 
if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0)) 
{ 
Write-Progress -Activity "Loading Modules" -Status "Loading Microsoft.SharePoint.PowerShell" 
Add-PSSnapin Microsoft.SharePoint.PowerShell 
} 


$SourceWebURL = "http://sitecollection/sites/site" 
$SourceLibraryTitle = "libraryName" 


$ssite = Get-SPSite $SourceWebURL
$sweb = $ssite.OpenWeb("subsite")



#$sWeb = Get-SPWeb $SourceWebURL 
$sList = $sWeb.Lists | ? {$_.Title -eq $SourceLibraryTitle} 


$spFieldType = [Microsoft.SharePoint.SPFieldType]::Text
$sList.Fields.Add("YourColumn",$spFieldType,$false)
$sList.Update()

Column is getting created but it is not showing in the library

  • Can you share more details ? What's the error message ? – KhalilG Mar 12 '15 at 9:45
  • Please review my updated code – Aquarius24 Mar 12 '15 at 10:00
3

I have tested your script and the column has been added successfully. The only thing that I has to do is to add the column to the current view. Check in your list settings that the column has been added. If you want to add the column automatically to the current view, you can add this to the end of your script :

# Get current view
$view = $sList.DefaultView

$newColumn = $sList.Fields["YourColumn"]

# Delete if already exist
while($view.ViewFields.ToStringCollection().Contains($column))
{

    $view.ViewFields.delete($newColumn)
    $view.Update()

}

# Add column
if(!$view.ViewFields.ToStringCollection().Contains($column))
{

    $view.ViewFields.add($newColumn)
    $view.Update()

}
  • Please Indicate "Mark as Answer" if the has Answered the Question – KhalilG Mar 12 '15 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.