C# 如何向sharepoint中的查找字段添加值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12560911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a value to a lookup field in sharepoint?
提问by Guruparan Giritharan
Hi I have two Lists in sharepoint 2007. I have a lookup column in on list which looks the other field. I want to use the sharepoint object model to add an item to the second list. How to i set the lookup field value. (The value is already in the other list).?
嗨,我在 sharepoint 2007 中有两个列表。我在列表中有一个查找列,它看起来像另一个字段。我想使用 sharepoint 对象模型将一个项目添加到第二个列表中。如何设置查找字段值。(该值已在另一个列表中)。?
SPListItem Employee = web.Lists["Employee"].Items.Add();
Employee["Name"] = account.Name;
Employee["Department"] = <lookup value must come here>
Employee.Update();
采纳答案by Spontifixus
Lookup fields will contain a combination of the row's id and the value of the column to display, separated by :#, in your case that could be 1:#HumanResourcesor 12:#Engineering.
查找字段将包含行的 id 和要显示的列的值的组合,以 分隔:#,在您的情况下可以是1:#HumanResources或12:#Engineering。
So to reference a lookup simply setting the id won't be enough, instead the above mentioned string needs to be set. Luckily SharePoint provides the class SPFieldLookupValuethat does exactly this:
因此,要引用查找,仅设置 id 是不够的,而是需要设置上述字符串。幸运的是,SharePoint 提供的类SPFieldLookupValue正是这样做的:
var department = web.Lists["Department"].GetItemById(1);
var employee = web.Lists["Employee"].Items.Add();
employee["Name"] = account.Name;
employee["Department"] = new SPFieldLookupValue(department.ID, department.Title);
employee.Update();

