Scenario:
updating choice column option value for all the columns in a site collection including sub sites.
Powerhsell Script:
param($url = $(Read-Host -prompt "Root Site Collection Url"))
#Get the PowerShell Snapin
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
#Get Root Site
$root = Get-SPSite $url
#If site was found / valid
if($root -ne $null)
{
foreach($subSite in $root.AllWebs)
{
$subSiteTitle = $subSite.Title
Write-Host $subSiteTitle -ForegroundColor Magenta
$subSiteURL = $subSite.Url
Write-Host $subSiteURL -ForegroundColor Cyan
$tempListArray=$subSite.lists
foreach($list in $tempListArray)
{
if( $list.Fields.ContainsField("choiceColumnName") -eq $true)
{
$ChoiceField = $list.Fields.GetField("choiceColumnName") #Get the field
if($ChoiceField.Choices.contains("Choice 1"))
{
Write-Host "Updating Cloumn:Security Classification" -ForegroundColor Red
$ChoiceField.Required=$true
$ChoiceField.Choices.Remove("Choice 1")
$ChoiceField.Choices.Add("Option 1")
$ChoiceField.update()
Write-Host "Updated List :" $list.Title
Write-Host "Updated Site " $subSite.Title
break;
}
}
}
$subSite.Dispose()
}
$root.Dispose()
}
updating choice column option value for all the columns in a site collection including sub sites.
Solution;
Below script will iterate through all the sites and sub sites of site collection and finds the choice column "choiceColumnName" and will change the value "Choice 1" to "Option 1".
Powerhsell Script:
param($url = $(Read-Host -prompt "Root Site Collection Url"))
#Get the PowerShell Snapin
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
#Get Root Site
$root = Get-SPSite $url
#If site was found / valid
if($root -ne $null)
{
foreach($subSite in $root.AllWebs)
{
$subSiteTitle = $subSite.Title
Write-Host $subSiteTitle -ForegroundColor Magenta
$subSiteURL = $subSite.Url
Write-Host $subSiteURL -ForegroundColor Cyan
$tempListArray=$subSite.lists
foreach($list in $tempListArray)
{
if( $list.Fields.ContainsField("choiceColumnName") -eq $true)
{
$ChoiceField = $list.Fields.GetField("choiceColumnName") #Get the field
if($ChoiceField.Choices.contains("Choice 1"))
{
Write-Host "Updating Cloumn:Security Classification" -ForegroundColor Red
$ChoiceField.Required=$true
$ChoiceField.Choices.Remove("Choice 1")
$ChoiceField.Choices.Add("Option 1")
$ChoiceField.update()
Write-Host "Updated List :" $list.Title
Write-Host "Updated Site " $subSite.Title
break;
}
}
}
$subSite.Dispose()
}
$root.Dispose()
}
Comments
Two questions:
Will this work against a doc library of more than 5000 items?
Can this script add a new value as well as changing an existing one?
Thx,
Barry