SPFieldMultiChoiceValue 类

包含一个SPFieldMultiChoice对象的值。

继承层次结构

System.Object
  Microsoft.SharePoint.SPFieldMultiChoiceValue

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
<SerializableAttribute> _
Public Class SPFieldMultiChoiceValue
用法
Dim instance As SPFieldMultiChoiceValue
[SerializableAttribute]
public class SPFieldMultiChoiceValue

示例

下面的示例是一个控制台应用程序,它演示了如何使用SPFieldMultiChoiceValue对象来设置和获取允许多个值的选择字段的值。

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

线程安全性

该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。

另请参阅

引用

SPFieldMultiChoiceValue 成员

Microsoft.SharePoint 命名空间

Value

SPFieldMultiChoice