SPFieldLookupValue class

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();
                        }
                    }
                }
            }
        }
    }
}
posted @ 2013-09-11 16:26  TwinStudio  阅读(186)  评论(0编辑  收藏  举报