六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 755|回复: 0

向SharePoint 2010一个带有Lookup字段的List中添加新item

[复制链接]

升级  32%

4

主题

4

主题

4

主题

童生

Rank: 1

积分
16
 楼主| 发表于 2013-1-6 05:20:09 | 显示全部楼层 |阅读模式
向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 //......
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表