SPFieldLookupValue對象即為查閱項的值,它有兩個屬性,分別是LookupId和LookupValue,LookupId為此查閱項的值在資料來源List中的ItemID,LookupValue為此查閱項的值在資料來源List中所對應的值,一般構造語句為SPFieldLookupValue myValue = new SPFieldLookupValue(LookupId, LookupValue),其預設Display出來的值得格式為“LookupId,#;LookupValue”
using System;using Microsoft.SharePoint; namespace ConsoleApp{ class Program { static void Main(string[] args) { using (SPSite site = new SPSite("http://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(); } } } } } }}