深入浅出SharePoint—对不同类型的字段进行赋值

来源:互联网 发布:淘宝联盟 pid 编辑:程序博客网 时间:2024/05/19 00:55

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();                        }                    }                }            }        }    }}


原创粉丝点击