1

I am currently trying to grab certain values(choice column) from a SP list into a DropdownList in Visual Studio, for example, currently in the SP List choice column, there're 5 choices altogether but I want to grab 3 out of the 5 choice and put it in a DropdownList in my Visual Studio form. Below is what I have tried so far with no success:

 protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            using (SPSite site = new SPSite("http://developmentserver.bwn"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["KPI Master"];
                    SPQuery query = new SPQuery();
                    query.Query = "<Where><Eq><FieldRef Name='CarType' /><Value Type='Choice'>KIA</Value></Eq></Where>";


                    SPListItemCollection Items = list.GetItems(query);

                    if (Items.Count > 0)
                    {

                        foreach (SPListItem Item in Items)
                        {
                            ddlDivision.DataSource = list.Items;
                            ddlDivision.DataBind();
                        }
                    } 
                }
            }

Can someone help an amateur SP Developer like me please?

Would really appreciate it.

  • Do you want to get the list items from list or choices/options from choice field in list?? – Ganesh Sanap Dec 19 '19 at 7:56
  • From a choice field in list. I google it and tried CAML Query to no extend, please help. – SPBoy Dec 19 '19 at 7:57
3

In your code, you don't need to add ddlDivision.DataBind(); inside foreach

foreach (SPListItem Item in Items)
      {
           ddlDivision.DataSource = list.Items;
           ddlDivision.DataBind();
      }

Try to loop for each item, then add it to the dropdown list as below

foreach (SPListItem Item in Items)
{
            ddlDivision.Items.Add(new ListItem(Item.Title));
}

The second option, try to remove foreach and set the data source for items

ddlDivision.DataSource = Items;
ddlDivision.DataTextField = "CarType";
ddlDivision.DataValueField = "CarType";
ddlDivision.DataBind();

If it's not working, Please, check

  • Thanks for the answer with options, i tried the first option adding the Item,Add into my foreach funtion and it work. But the value in my dropdownlist becomes 'Microsoft.SharePoint.SPListItemCollection' instead of names from the SP List Choice. Why is that kind sir? – SPBoy Dec 19 '19 at 8:19
  • I have updated my answer, please check the first option using ddlDivision.Items.Add(new ListItem(item.Title)); if it's not worked have a look to the second option, please feedback us with the result and if it's working for you don't forget to Mark the answer as Accepted :) – Mohamed El-Qassas MVP Dec 19 '19 at 8:27
  • Well that works a treat, the first option. Thanks so much for the answer kind sir. – SPBoy Dec 21 '19 at 0:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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