Export (0) Print
Expand All

SPFieldMultiChoiceValue Class

Contains the value for an SPFieldMultiChoice object.

System.Object
  Microsoft.SharePoint.SPFieldMultiChoiceValue

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

[SerializableAttribute]
[SubsetCallableTypeAttribute]
public class SPFieldMultiChoiceValue

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("http://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();
        }
    }
}

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

Community Additions

ADD
Show:
© 2014 Microsoft