3

In my powershell script I want to fetch the value of a mulit-choice-field in a sharepoint list.

The problem is that although the value is returned, it is encased in ;# - for example, I would receive ;#myValue;# instead of just myvalue. I know these characters are added by Sharepoint behind the scenes because it is a choice field.

Now, I know how to remove this manually:

$fieldlookup = $fieldlookup.replace(";#","")

But is there a way that Sharepoint will return the value properly?

Here is my code:

$web = Get-SPWeb $portal_url
$list = $web.Lists[$portal_list]
$items = $list.items | sort-object -property "ID"

foreach($item in $items){   
    $fieldlookup = new-object Microsoft.SharePoint.SPFieldMultiChoiceValue($item["multichoiceColumntitle"].toString() )
    write-host $fieldlookup
}

In fact, it doesn't even seem to make any difference whether I do this:

$fieldlookup = new-object Microsoft.SharePoint.SPFieldMultiChoiceValue($item["multichoiceColumntitle"].toString() )

or this:

$fieldlookup = $item["multichoiceColumntitle"]
4

Multi-choice field values are stored internally in ArrayList

How to get multi-choice field values in PowerShell

$web = Get-SPWeb $Url
$list = $web.Lists[$ListTitle]
$item = $list.GetItemById($ItemId)
$values = new-object Microsoft.SharePoint.SPFieldMultiChoiceValue($item[$fieldName])
//print multi-choice field values
for($i=0;$i -lt $values.Count;$i++)            
{            
   Write-Host $values[$i]            
}

Update

In order to convert SPFieldMultiChoiceValue to string without delimiters you could use the following way:

$values = new-object Microsoft.SharePoint.SPFieldMultiChoiceValue($item[$fieldName])
$sValues = $values.ToString().Replace([Microsoft.SharePoint.SPFieldMultiChoiceValue]::Delimiter,"") 

or like this:

if($item[$fieldName]) {
   $sValues = $item[$fieldName].Replace([Microsoft.SharePoint.SPFieldMultiChoiceValue]::Delimiter,"")
}
  • That fetches all the values in the choice, but the problem is getting the selected value without the additional characters. – bgmCoder Aug 21 '14 at 20:09
  • 1
    so,you would like to convert to string value but without delimiters, right? If so, please check the updated answer – Vadim Gremyachev Aug 21 '14 at 20:56
  • 1
    Keep in mind this only works with one value, and you may end up with a bunch of values all strung together. – BlueBird Aug 21 '14 at 21:06
  • 1
    The value of this method is that you don't have to know what the delimiters are. – bgmCoder Aug 21 '14 at 21:17
  • 1
    That's right, regarding checking for null values, please check the updated answer again (last snippet) – Vadim Gremyachev Aug 21 '14 at 21:21
0

Get a multi-choice field

  $multichoicevalues = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue($i["multiplechoicefield"].ToString());            
    $multichoicevalues.ToString();            
    for($c=0;$c-lt$multichoicevalues.Count;$c++)            
    {            
     $multichoicevalues[$c]            
    };

Here are the both behavour shown in below screen shot. enter image description here

this technet talk about other possible situations.

http://social.technet.microsoft.com/wiki/contents/articles/20831.sharepoint-a-complete-guide-to-getting-and-setting-fields-using-powershell.aspx#Set_and_Get_a_Multi-Choice_Field

  • 1
    How is that code any different that what I have written except that it iterates through all the list values? (I only want the selected value anyway) – bgmCoder Aug 21 '14 at 19:58
  • 1
    It is my understanding the SPFieldMultiChoiceValue returns the selected values, and thus you should be iterating through all of them. Other wise you might be using the wrong type of field. If you see in the example above, you can see if you call the value using the index, you should get the value without delimiters. – BlueBird Aug 21 '14 at 21:01
  • 1
    But in my question, I am using SPFieldMultiChoiceValue on a single selected value, and it returns the string with the delimiters. If I have to iterate through all possible values for the field, how do I know which one is selected? – bgmCoder Aug 21 '14 at 21:19

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.