I have a dropdown with values from a column named Title(Single line of text)
The end goal is to store these values from the dropdown to a lookup column named Team.
Read a few articles here , However did not find a resolution to my issue.
C# Code
private void AddEmployee()
{
try
{
string login = "******";
string password = "******";
SecureString securepassword = new SecureString();
foreach (char c in password)
{ securepassword.AppendChar(c); }
ClientContext ctx = new ClientContext("https://site.sharepoint.com/sites/test");
ctx.Credentials = new SharePointOnlineCredentials(login, securepassword);
Web site = ctx.Web;
List list = site.Lists.GetByTitle("Employee");
CamlQuery query = CamlQuery.CreateAllItemsQuery();
var items = list.GetItems(query);
ctx.Load(items);
ctx.ExecuteQuery();
foreach (var oListItem in items)
{
oListItem["Title"] = txtName.Text.ToString();
oListItem["Age"] = txtAge.Text;
oListItem["Address"] = txtAddress.Text;
FieldLookupValue lookup = oListItem["Team"] as FieldLookupValue;
lookup.LookupId = team.SelectedIndex;
oListItem["Team"] = lookup;
oListItem.Update();
}
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Data Inserted Successfully...');", true);
}
catch (ApplicationException ex)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "ErrorAlert", "Error Occured" + ex.Message, true);
}
}
The code runs without any errors , However no data is saved to the respective columns.
Would appreciate if anyone could assist me.
Update
Modified the code as below , However no values are saved
FieldLookupValue lookup = oListItem["Team"] as FieldLookupValue;
lookup.LookupId = team.SelectedIndex;
oListItem["Team"] = lookup;
Also wrote the ctx.ExecuteQuery();
before ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Data Inserted Successfully...');", true);
and got the error
The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.