Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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"]
share|improve this question

2 Answers 2

up vote 3 down vote accepted

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,"")
}
share|improve this answer
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 at 20:56
1  
That's it! Thanks! –  BGM Aug 21 at 21:01
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 at 21:06
1  
Thanks, fellas. I appreciate the comments. –  BGM Aug 21 at 21:09
1  
The value of this method is that you don't have to know what the delimiters are. –  BGM Aug 21 at 21:17

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

share|improve this answer
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) –  BGM Aug 21 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 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? –  BGM Aug 21 at 21:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.