我有一个员工列表,其中包含2列(部门和经理),这些列是查阅列。当新用户注册时,我需要代码来更新这些查找列。 (虽然其他列正在列表中更新)
以下是代码:
using (ClientContext clientContext = new ClientContext("https://sp13appstoredev.xyz.com/sites/DevApps/TrainingSite/"))
{
clientContext.Load(clientContext.Web, web => web.Title);
List oList = clientContext.Web.Lists.GetByTitle("Employees");
clientContext.Load(oList);
ListItemCreationInformation itemInfo = new ListItemCreationInformation();
Microsoft.SharePoint.Client.ListItem myItem = oList.AddItem(itemInfo);
myItem["Title"] = txtFirstName.Text;
myItem["Last_x0020_Name"] = txtLastName.Text;
myItem["u5ib"] = txtAge.Text;
myItem["Address"] = txtAddress.Text;
//FieldLookupValue lookUpDepartment = new FieldLookupValue();
//myItem["Department"] = lookUpDepartment as FieldLookupValue;
//FieldLookupValue lookUpManager = new FieldLookupValue();
//myItem["Manager"] = lookUpManager as FieldLookupValue;
myItem["Gender"] = radioBtnGender.Text;
myItem["Salary"] = txtSalary.Text;
myItem.Update();
clientContext.ExecuteQuery();
}
我评论了查询列行,以检查列表中是否正在更新其他列。
请帮助。谢谢:))
答案 0 :(得分:1)
添加以下行:
myItem["Department"] = 1;
myItem["Manager"] = 1;
我还必须将父列表设置为ID(之前它只是标题)
答案 1 :(得分:0)
这是一个更好的解决方案:
//Department column
FieldLookupValue deptItem = new FieldLookupValue();
deptItem.LookupId = Convert.ToInt32(DropDownListDepartment.SelectedValue);
myItem["Department"] = deptItem;
myItem.Update();
//Manager column
FieldLookupValue managerItem = new FieldLookupValue();
managerItem.LookupId = Convert.ToInt32(DropDownListManager.SelectedValue);
myItem["Manager"] = managerItem;
myItem.Update();