Thursday, September 26, 2013

How To Add a Site Column to All Document Library available in Site Collection using Powershell

Requirement: Need to Add a custom Choice column to all available Document Libraries in a Site Collection

Solution:

Get-SPWeb -site   http://mywebapp -Limit All | foreach {
$doclibtype = [Microsoft.SharePoint.SPBaseType]::DocumentLibrary
$doclibs = $_.GetListsOfType($doclibtype)
$doclibs.Count
for ($i=0; $i -lt $doclibs.Count; $i++)
{
$doclib = $doclibs[$i]
$doclib.Title
$Choices = New-Object System.Collections.Specialized.StringCollection
$Choices.Add("Choice A")
$Choices.Add("Choice B")
$Choices.Add("Choice C")
$fieldtype = [Microsoft.SharePoint.SPFieldType]::Choice
$doclib.Fields.Add(“My Column”,$fieldtype,$True,$false,$choices)
$doclib.Update()
$view = $doclib.Defaultview
$view.viewFields.Add("My Column")
$view.Update()
}
}
$site.Dispose()

2 comments:

  1. This adds a Choice Column not a Site Column. How would you modify the code to do a Site column insert?

    ReplyDelete
  2. Thanks for sharing this useful information. Speaking about technologies these days. I know that virtual data rooms are good substitute for old-fashioned way of storing the information.

    ReplyDelete