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 read update choice field value


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.brightpoint.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!"
Get-Set Choice Field Value in SharePoint using PowerShell Get-Set Choice Field Value in SharePoint using PowerShell Reviewed by Salaudeen Rajack on April 23, 2017 Rating: 5

6 comments:

  1. Good post! I definitely learn something new and challenging on a daily basis from your blog, Thanks a bunch buddy.

    ReplyDelete
  2. The value can not be reflected in sharepoint online ?

    ReplyDelete
  3. I can not use ".ParseAndSetValue" with sharepoint online.

    ReplyDelete
  4. SharePoint Online: Is this a problem? Type [Microsoft.SharePoint.SPFieldMultiChoiceValue] not found

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.