Bind a <asp:DropDownList> to a SharePoint List


This may be a bit too simple for most SharePoint developers but as I struggled to find the correct code I’ll drop it here, perhaps someone will get a hit if they search for the same thing…

What I wanted to achieve was to have a DropDownList which automatically retrieved its value from a SharePoint list. I wanted to I could have written a foreach loop to solve this but I knew data binding was the way to go. Include the code below in your Page_Load method and the list will be populated.

using (SPSite site = new SPSite("http://devserver/somesite"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists["Departments"];
        cboDepartment.DataSource = list.Items;
        cboDepartment.DataValueField = "Title"; // List field holding value
        cboDepartment.DataTextField = "Title"; // List field holding name to be displayed on page
        cboDepartment.DataBind();
    }
}

Comments (9)

  1. ashok_jingar@yahoo.co.uk says:

    Hi Mattias,

    Thank you very much for this – you solved my query!

    One thing I noticed (and others might also benefit from this), is that the list values are not retained if you have other fields that are required and have validation. So the following solves that:

    if (!Page.IsPostBack)

           {

               using (SPSite site = new ("http://yoursharepointsite&quot;))

               {

                   using (SPWeb web = site.OpenWeb())

                   {

                       SPList list = web.Lists["NameOfYourList"];

                       dropSite.DataSource = list.Items;

                       dropSite.DataValueField = "Title";

    dropSite.DataTextField = "Title";

                       dropSite.DataBind();

                   }

               }

           }

  2. JLou says:

    Thank you, I would have written a full-blown DOM-appending script without knowing about "DataValueField" and "DataTextField"!

  3. swordfish8 says:

    Hi mattlind / Jlou

    I have two choice columns in my list

    ContactType (Drop-Down Menu)

    Direction (Drop-Down Menu)

    Name of the List: Contact

    I want to populate two seperate drop downs in my web part one for contact type and the other for Direction

    how can I do this, please help

    Thanks

  4. Naresh says:

    Hi,

    how can i get the field values other than Title, for instance if i have a column name called column1 of type text, how can i get those values in drop down, i tried using the display name and intername in above code without any luck.

    also I am trying to achieve cascaded drop down lists in asp.net form while sources being taken from share point list, please throw give some focus on this if you have any ideas.

    please share, thanks,

    Naresh

  5. soni says:

    hi,

    how can i bind a menu control to a sharepoint list??

    anyone any idea please share asap

  6. Biswadeep Chakraborty says:

    Hi,

    If you want to use custom columns other than "Title" and "ID"  

    Change the line

    cboDepartment.DataSource = list.Items;

    to

    cboDepartment.DataSource = list.Items.GetDataTable();

    and you can have your custom columns as text and value.

    Thanks

    Biswa

  7. sunil says:

    cboDepartment.DataValueField="text "+","+"text ";

    i want to achieve this how to do it. plz help me out

  8. panimoss says:

    I am not to bind the custom list to dropdown list.

    The above posted code is not working for me. why any idea? please help?