I am using asp.net grid to display and perform CRUD operations on a SharePoint list. And I'm unable to update values to the list.
Upon using the the following code where manager is the lookup field in the Employee list, I get the error:
Index was outside the bounds of the array.
public void updateRow(string itemID, string firstName, string lastName, string age, string eAddress, string department, string manager, string gender, string salary)
{
ClientContext clientContext = new ClientContext("https://xyz.xyz.com/sites/xyz/TrainingSite/");
try
{
SP.List oList = clientContext.Web.Lists.GetByTitle("Employees");
clientContext.Load(oList);
SP.CamlQuery camlQuery = new SP.CamlQuery();
camlQuery.ViewXml = @"<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + itemID + "</Value></Eq></Where></Query>";
SP.ListItemCollection itemInfo = oList.GetItems(camlQuery);
clientContext.Load(itemInfo);
clientContext.ExecuteQuery();
foreach (SP.ListItem item in itemInfo)
{
if (itemID == item["ID"].ToString())
{
item["Title"] = firstName;
item["Last_x0020_Name"] = lastName;
item["u5ib"] = age;
item["Address"] = eAddress;
//Department column
//item["Department"] = department;
FieldLookupValue deptItem = new FieldLookupValue();
//deptItem.LookupId = Convert.ToInt32(department); //department should be department list item ID because its a lookup ID
item["Department"] = deptItem;
department = department.Split('#')[0]; //throws index was outside the bounds of the array error
deptItem.LookupId = Convert.ToInt32(department);
item["Department"] = deptItem;
//Manager column
//item["Manager"] = manager;
FieldLookupValue mgrItem = new FieldLookupValue();
manager = manager.Split('#')[0];//throws index was outside the bounds of the array error
mgrItem.LookupId = Convert.ToInt32(manager);
item["Manager"] = mgrItem;
item["Gender"] = gender;
item["Salary"] = salary;
item.Update();
break;
}
}
clientContext.ExecuteQuery();
}
catch (Exception e)
{
throw e;
}
}
I am using a sharepoint list(employee) that contains 2 lookup columns(department and manager). The above code is to update the sharepoint list
Kindly help! Thanks! :)