Get-Set Choice Field Value in SharePoint using PowerShell
Here is my nifty collection of PowerShell scripts to read or update values in Choice column in SharePoint.
SharePoint: PowerShell to Get Choice Field Value
PowerShell to get choice field values
PowerShell to Get Multiple Choices Field Value
We can get choice field value programmatically using this PowerShell script.
SharePoint: Set Choice Field Value using PowerShell
This PowerShell script adds value to choice column
Set Multi-Choice Field value using PowerShell:
This Sets multi choice field programmatically
Update Choice Field Check boxes (allow multiple selections) Values:
Here is an another method to set multi choice column.
SharePoint: PowerShell to Get Choice Field Value
PowerShell to get choice field values
#Define URL and List name variables $WebURL="http://intranet.crescent.com/" $ListName ="Projects" $Web = Get-SPWeb $WebURL $List = $Web.lists.TryGetList($ListName) #Get the 1st item stored in the list $Item = $list.items[0] #Get the formatted value of "Department" choice column Write-host "Choice Field Value:" $item.GetFormattedValue("Department")
PowerShell to Get Multiple Choices Field Value
We can get choice field value programmatically using this PowerShell script.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue $WebURL="http://intranet.crescent.com/" $ListName ="Projects" $MultiChoiceFieldName="Category" $Web = Get-SPWeb $WebURL $List = $Web.lists.TryGetList($ListName) #Get the 1st item stored in the list $Item = $List.items[0] #Get the multiple choice field $MultiChoiceValues = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue($Item[$MultiChoiceFieldName]) #Print Each Value For($I=0; $I -lt $MultiChoiceValues.count; $I++) { write-host $MultiChoiceValues[$I] }
SharePoint: Set Choice Field Value using PowerShell
This PowerShell script adds value to choice column
#Define URL and List name variables $WebURL="http://sharepoint.crescent.com/sites/Operations/" $ListName ="List Versions" #Get the Web and Lists objects $web = Get-SPWeb $WebURL $List = $web.Lists[$ListName] #Add new Get the 1st item stored in the list $Item = $list.AddItem() $item["Title"] = "Test001" #Set Mandatory title field #sharepoint 2010 powershell update choice field $item["Department"] = "Sales" $item.Update(
Set Multi-Choice Field value using PowerShell:
This Sets multi choice field programmatically
#Add new Get the 1st item stored in the list $Item = $list.AddItem() $item["Title"] = "Test009" #Set Choice values $choicevalues = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue $choicevalues.Add("Sales") $choicevalues.Add("Purchase") $list.Fields["Department"].ParseAndSetValue($item,$choicevalues) #Commit changes $item.Update()This sets value of choice field.
Update Choice Field Check boxes (allow multiple selections) Values:
Here is an another method to set multi choice column.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue #Define URL and List name variables $WebURL="http://portal.crescent.com/sites/ops" $ListName ="News" #Get the Web and List and Item objects $web = Get-SPWeb $WebURL $List = $web.Lists[$ListName] $Item = $List.GetItemById(1) #Set Choice values $choicevalues = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue $choicevalues.Add("News") $choicevalues.Add("Flash News") #sharepoint Powershell to update choice field $item["Category"] = $Choicevalues $item.Update() Write-host "Choice field values updated for item!"
Good post! I definitely learn something new and challenging on a daily basis from your blog, Thanks a bunch buddy.
ReplyDeleteThe value can not be reflected in sharepoint online ?
ReplyDeleteI can not use ".ParseAndSetValue" with sharepoint online.
ReplyDeleteSharePoint Online: Is this a problem? Type [Microsoft.SharePoint.SPFieldMultiChoiceValue] not found
ReplyDeleteThis script is written for SharePoint On-premises. For SharePoint Online, use: How to Get-Set Choice Field Value in SharePoint Online using PowerShell?
DeleteThank you. understood.
ReplyDeleteHow would l update the choice associated to content type as the changes have been made locally to the list but not to the content type
ReplyDelete