向SharePoint 2010一个带有Lookup字段的List中添加新item
向SharePoint 2010一个带有Lookup字段的List中添加新item<div class="postbody"><div id="cnblogs_post_body">为了解决这个问题,网上搜集到的资料:
1。 这个连接里面建议用SPFieldLookupValue
http://sharepoint.stackexchange.com/questions/8045/add-an-item-with-lookup-field-using-object-model-sp2010
2。这个地址是1中那位大哥推荐的Sample:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldlookupvalue.aspx
根据这两条消息,我们于是可以很快找到解决方案:
1,先找到你要Lookup的那个item的ID (int 类型)
2,然后用SPFieldLookupValue进行新数据插入操作。
CodeSnippet:
<div class="cnblogs_code"> 1 SPList theListThatContainALookupField = GetTheList(); 2 SPListItem newItem = theListThatContainALookupField.Items.Add(); 3 4 // to find the ID of the lookup item 5 int lookupid = -1; 6 SPList anotherList = GetTheList(); 7 foreach(SPListItem item in anotherList.GetItems()) 8 { 9 if( anycondition....)10 lookupid = item.ID;11 }12 newItem["otherFields"] = "AnyValue";13 newItem["LookupField"] = new SPFieldLookupValue(lookupid, "AddedFieldValue");14 newItem.Update();15 //......
页:
[1]