Partager via


SPFieldMultiChoiceValue - Classe

Contient la valeur d'un objet SPFieldMultiChoice .

Hiérarchie d’héritage

System.Object
  Microsoft.SharePoint.SPFieldMultiChoiceValue

Espace de noms :  Microsoft.SharePoint
Assembly :  Microsoft.SharePoint (dans Microsoft.SharePoint.dll)

Syntaxe

'Déclaration
<SerializableAttribute> _
Public Class SPFieldMultiChoiceValue
'Utilisation
Dim instance As SPFieldMultiChoiceValue
[SerializableAttribute]
public class SPFieldMultiChoiceValue

Exemples

L'exemple suivant est une application de console qui montre comment utiliser un objet SPFieldMultiChoiceValue pour définir et obtenir la valeur d'un champ choix acceptant des valeurs multiples.

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

Cohérence de thread

Tous les membres statique (Partagé dans Visual Basic)s publics de ce type sont thread-safe. Cela n’est pas garanti pour les membres d’instance.

Voir aussi

Référence

SPFieldMultiChoiceValue - Membres

Microsoft.SharePoint - Espace de noms

Value

SPFieldMultiChoice