0

I am trying to bind a lookup column named team to an asp.net dropdown and the dropdown renders blank

HTML

<asp:DropDownList ID="team" runat="server" AutoPostBack="true"></asp:DropDownList>

CSOM

private void FetchTeam()
        {
            if (!Page.IsPostBack)
            {
                using (ClientContext ctx = new ClientContext("https://site.sharepoint.com/sites/test"))
                {

                    string login = "*****";
                    string password = "******";
                    SecureString securepassword = new SecureString();

                    foreach (char c in password)

                    { securepassword.AppendChar(c); }



                    ctx.Credentials = new SharePointOnlineCredentials(login, securepassword);

                    Web site = ctx.Web;
                    List list = site.Lists.GetByTitle("Employee");
                    CamlQuery query = CamlQuery.CreateAllItemsQuery();
                    Microsoft.SharePoint.Client.ListItemCollection collection = list.GetItems(query);
                    ctx.Load(collection);

                    ctx.ExecuteQuery();

                    foreach (Microsoft.SharePoint.Client.ListItem item in collection)
                    {
                        string text = string.Format("{0}", item.FieldValues["Team"]);
                        eid.Items.Add(new System.Web.UI.WebControls.ListItem(text, item.Id.ToString()));
                    }
                }
            }
        }

All the articles here are for SharePoint Server SSOM.

Would appreciate if anyone could help me with this.

1 Answer 1

1

Kindly use the below code snippet for assign Team Look-up column value in the drop-down.

foreach (Microsoft.SharePoint.Client.ListItem item in collection)
{
    FieldLookupValue lookup = item["Team"] as FieldLookupValue;
    string lvalue = lookup.LookupValue;
    int lId = lookup.LookupId; // If need to bind the look-up column field value's ID in the drop-down as ID then use this.

    eid.Items.Add(new System.Web.UI.WebControls.ListItem(lvalue, item.Id.ToString()));
}
5
  • Hi Divya , This seems to be working as expected :) Thanks a lot. Since I am new to SharePoint Dev , do you have any article that focusses on basic CRUD operations using CSOM in SharePoint Online? any help would be appreciated. If you want me to post this as separate question please let me know.
    – Vivek
    Commented May 13, 2021 at 8:08
  • 1
    Hello Vivek, please refer the below links for some basics operation through CSOM: c-sharpcorner.com/UploadFile/sagarp/… enjoysharepoint.com/sharepoint-net-client-object-model-examples Commented May 13, 2021 at 9:38
  • Thanks for the help Divya , would surely take a look :)
    – Vivek
    Commented May 13, 2021 at 10:18
  • Hi Divya , came across another issue pertaining to saving values to a lookup column from a dropdown , would appreciate if you could take a look. Thanks :) sharepoint.stackexchange.com/questions/293613/…
    – Vivek
    Commented May 13, 2021 at 16:52
  • 1
    Hello Vivek, I have provided my response on the above question. Please have a look on it. Commented May 14, 2021 at 4:38

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.