如何在sharepoint中的查找字段中添加值?

Gur*_*ran 5 c# sharepoint moss sharepoint-2007

嗨我在sharepoint 2007中有两个列表.我在列表中有一个查找列,它看起来是另一个字段.我想使用sharepoint对象模型将项添加到第二个列表.如何设置查找字段值.(该值已在另一个列表中).

SPListItem Employee = web.Lists["Employee"].Items.Add();
Employee["Name"] = account.Name;
Employee["Department"] = <lookup value must come here>
Employee.Update();                 
Run Code Online (Sandbox Code Playgroud)

Spo*_*xus 5

查找字段将包含行的 id 和要显示的列的值的组合,以 分隔:#,在您的情况下可以是1:#HumanResources12:#Engineering

因此,要引用查找,仅设置 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(); 
Run Code Online (Sandbox Code Playgroud)