SPFieldMultiChoiceValue class

Contains the value for an SPFieldMultiChoice object.

Inheritance hierarchy

System.Object
  Microsoft.SharePoint.SPFieldMultiChoiceValue

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
<SerializableAttribute> _
Public Class SPFieldMultiChoiceValue
'Usage
Dim instance As SPFieldMultiChoiceValue
[SerializableAttribute]
public class SPFieldMultiChoiceValue

Examples

The following example is a console application that demonstrates how to use an SPFieldMultiChoiceValue object to set and get the value of a choice field that allows multiple values.

using System;
using System.Collections.Specialized;
using Microsoft.SharePoint;

namespace ConsoleApp
{
    class Program
    {
        static string listTitle = "My Custom List";
        static string fieldTitle = "Gift Options";
        static string fieldInternalName = null;

        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://localhost"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    SPList list = web.Lists.TryGetList(listTitle);
                    if (list != null)
                    {
                        // Add a multichoice field to the list.
                        StringCollection choices = new StringCollection();
                        choices.Add("Gift wrap");
                        choices.Add("Gift card");
                        choices.Add("Include gift receipt");

                        fieldInternalName = list.Fields.Add(fieldTitle, SPFieldType.MultiChoice, false, false, choices);
                        list.Update();

                        // Get a reference to the field.
                        SPFieldMultiChoice choiceField = (SPFieldMultiChoice)list.Fields.GetField(fieldInternalName);

                        // Create a field value with all choices selected.
                        // (A CheckBoxChoiceField control would have all boxes checked.)
                        SPFieldMultiChoiceValue values = new SPFieldMultiChoiceValue();
                        foreach (string choice in choices)
                        {
                            values.Add(choice);
                        }

                        // Add an item to the list.
                        SPListItem item = list.Items.Add();
                        item[SPBuiltInFieldId.Title] = "Test item";
                        item[choiceField.Id] = values;
                        item.Update();

                        // Get the value of the field in the item.
                        string rawValue = item[choiceField.Id].ToString();
                        SPFieldMultiChoiceValue typedValue = new SPFieldMultiChoiceValue(rawValue);

                        // Print the value.
                        Console.WriteLine("The raw value is {0}", rawValue);
                        Console.WriteLine("The value delimiter is {0}", SPFieldMultiChoiceValue.Delimiter);
                        for (int i = 0; i < typedValue.Count; i++)
                        {
                            Console.WriteLine("The value at index {0} is {1}", i, typedValue[i]); 
                        }
                    }
                }
            }
            Console.WriteLine("\nPress ENTER to continue....");
            Console.Read();
        }
    }
}
Imports System
Imports System.Collections.Specialized
Imports Microsoft.SharePoint

Module ConsoleApp

    Dim listTitle As String = "My Custom List"
    Dim fieldTitle As String = "Gift Options"
    Dim fieldInternalName As String = Nothing

    Sub Main()

        Using site As New SPSite("https://localhost")
            Using web As SPWeb = site.RootWeb

                Dim list As SPList = web.Lists.TryGetList(listTitle)
                If list IsNot Nothing Then

                    ' Add a multichoice field to the list.
                    Dim choices As New StringCollection()
                    choices.Add("Gift wrap")
                    choices.Add("Gift card")
                    choices.Add("Include gift receipt")

                    fieldInternalName = list.Fields.Add(fieldTitle, SPFieldType.MultiChoice, False, False, choices)
                    list.Update()

                    ' Get a reference to the field.
                    Dim choiceField As SPFieldMultiChoice = DirectCast(list.Fields.GetField(fieldInternalName), SPFieldMultiChoice)

                    ' Create a field value with all choices selected.
                    ' (A CheckBoxChoiceField control would have all boxes checked.)
                    Dim values As New SPFieldMultiChoiceValue()
                    For Each choice As String In choices
                        values.Add(choice)
                    Next

                    ' Add an item to the list.
                    Dim item As SPListItem = list.Items.Add()
                    item(SPBuiltInFieldId.Title) = "Test item"
                    item(choiceField.Id) = values
                    item.Update()

                    ' Get the value of the field in the item.
                    Dim rawValue As String = item(choiceField.Id).ToString()
                    Dim typedValue As New SPFieldMultiChoiceValue(rawValue)

                    ' Print the value.
                    Console.WriteLine("The raw value is {0}", rawValue)
                    Console.WriteLine("The value delimiter is {0}", SPFieldMultiChoiceValue.Delimiter)
                    For i As Integer = 0 To typedValue.Count - 1
                        Console.WriteLine("The value at index {0} is {1}", i, typedValue(i))
                    Next
                End If

            End Using
        End Using
        Console.WriteLine(vbLf & "Press ENTER to continue....")
        Console.Read()
    End Sub

End Module

Thread safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See also

Reference

SPFieldMultiChoiceValue members

Microsoft.SharePoint namespace

Value

SPFieldMultiChoice