0

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.

2
  • After the "oListItem.Update();" do you ctx.ExecuteQuery() your updates?
    – dinos.kon
    May 13, 2021 at 18:08
  • @dinos.kon , I tried that too and it gives me an 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."
    – Vivek
    May 14, 2021 at 7:04

2 Answers 2

0

I think you are missing ctx.ExecuteQuery(); after updating items.

Try adding ctx.ExecuteQuery(); before this line:

ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Data Inserted Successfully...');", true);
1
  • Hi Ganesh , thank you for responding. I tried this and it gives an 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."
    – Vivek
    May 14, 2021 at 7:03
0

Kindly use the below code snippet for setting the look-up field value:

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 = lID; // assign the selected value of the drop-down Team
    oListItem["Team"] = lookup;
    oListItem.Update();
}

Edit:

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");
        
        ListItemCreationInformation listCreationInformation = new ListItemCreationInformation();  
        ListItem oListItem = list.AddItem(listCreationInformation);  
        
        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();  
        ctx.ExecuteQuery();  
        
        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);
    }
}
4
  • Hi Divya ,thanks for responding , Tried the steps suggested by you , However no data is getting saved to the list. I am updating my question , Kindly check
    – Vivek
    May 14, 2021 at 9:15
  • Hello Vivek, I have updated my answer please check after "Edit". May 14, 2021 at 9:38
  • Hi Divya , I've tried this before and it gives me 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". Also it does not allow me to write ListItem oListItem =list.AddItem(listCreationInformation); . Replaced it with Microsoft.SharePoint.Client.ListItem oListItem = list.AddItem(listCreationInformation);
    – Vivek
    May 14, 2021 at 10:24
  • Have you installed Microsoft.SharePointOnline.CSOM? If not, then please refer the article for which NuGet package to use for Online. developer.microsoft.com/en-us/office/blogs/… May 14, 2021 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.