共用方式為


SPFieldLookupValue class

包含SPFieldLookup物件的值。

Inheritance hierarchy

System.Object
  Microsoft.SharePoint.SPFieldLookupValue
    Microsoft.SharePoint.SPFieldUserValue

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

Syntax

'宣告
Public Class SPFieldLookupValue
'用途
Dim instance As SPFieldLookupValue
public class SPFieldLookupValue

備註

這個類別的例項代表 「 查閱 」 欄位的單一欄位值。查閱欄位可以有多個值,或只是其中一個。您可以判斷這是藉由檢查 [查詢] 欄位的AllowMultipleValues屬性的值的大小寫。如果屬性會傳回true,欄位可以有多個值。如果它傳回false,不能。

如果 [查詢] 欄位不允許多個值,欄位值會是欄位的包含在 [查詢] 欄位所指向的點加上的 [查詢] 欄位所指向的項目中值的清單中的項目 ID 的型別String的物件。您可以將這個字串做為引數傳遞, SPFieldLookupValue(String)建構函式來建立SPFieldLookupValue物件。然後您可以獲得清單項目 ID LookupId屬性,並在清單項目,從LookupValue屬性欄位的值。

如果 [查詢] 欄位可讓多個值,欄位值則會被 box 為型別Object的型別SPFieldLookupValueCollection的物件。SPFieldLookupValueCollection物件是SPFieldLookupValue物件的集合。您可以藉由列舉集合及存取每個物件的LookupValue屬性來擷取欄位值。

Examples

下列範例會示範如何設定查詢欄位的值。

範例程式碼是主控台應用程式,取得兩個相關的清單,客戶和訂單的參考。在 [客戶] 清單中的每個項目代表客戶記錄,而項目中的 [識別碼] 欄位用來識別客戶。在 [訂貨主檔] 清單中的每個項目代表與客戶放置的順序。[訂貨主檔] 清單中有一個稱為 [客戶編號指到 [識別碼] 欄位,在 [客戶] 清單中,找出筆訂單的客戶的查閱欄位。

foreach迴圈中的程式碼逐一加入 [訂貨主檔] 清單中的新項目,每個客戶在客戶清單的客戶清單。在每個案例中,程式碼會設定 [查詢] 欄位中,要連結至客戶的訂單的客戶 ID 值。

using System;
using Microsoft.SharePoint;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://localhost"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    SPList customerList = web.Lists.TryGetList("Contoso Customers");
                    SPList orderList = web.Lists.TryGetList("Contoso Orders");

                    if (customerList != null && orderList != null)
                    {
                        SPListItemCollection customers = customerList.Items;
                        SPListItemCollection orders = orderList.Items;

                        string fieldName = "CustIDLookup";
                        if (!orderList.Fields.ContainsField(fieldName))
                            return;
                        
                        SPField lookupFld = orderList.Fields.GetField(fieldName);

                        foreach (SPListItem customer in customers)
                        {
                            SPListItem order = orders.Add();
                            order[SPBuiltInFieldId.Title] = "Thank you!";
                            order.Update();

                            SPFieldLookupValue value = new SPFieldLookupValue(customer.ID, customer.ID.ToString());
                            order[lookupFld.Id] = value.ToString();
                            order.Update();
                        }
                    }
                }
            }
        }
    }
}
Imports System
Imports Microsoft.SharePoint

Module ConsoleApp

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

                Dim customerList As SPList = web.Lists.TryGetList("Contoso Customers")
                Dim orderList As SPList = web.Lists.TryGetList("Contoso Orders")

                If customerList IsNot Nothing AndAlso orderList IsNot Nothing Then
                    Dim customers As SPListItemCollection = customerList.Items
                    Dim orders As SPListItemCollection = orderList.Items

                    Dim fieldName As String = "CustIDLookup"
                    If Not orderList.Fields.ContainsField(fieldName) Then
                        Return
                    End If

                    Dim lookupFld As SPField = orderList.Fields.GetField(fieldName)

                    For Each customer As SPListItem In customers
                        Dim order As SPListItem = orders.Add()
                        order(SPBuiltInFieldId.Title) = "Thank you!"
                        order.Update()

                        Dim value As New SPFieldLookupValue(customer.ID, customer.ID.ToString())
                        order(lookupFld.Id) = value.ToString()
                        order.Update()
                    Next
                End If

            End Using
        End Using
    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.

請參閱

參照

SPFieldLookupValue members

Microsoft.SharePoint namespace