Partilhar via


Bind a 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("https://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

  • Anonymous
    February 11, 2008
    PingBack from http://www.biosensorab.org/2008/02/12/bind-a-to-a-sharepoint-list/

  • Anonymous
    January 07, 2010
    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"))            {                using (SPWeb web = site.OpenWeb())                {                    SPList list = web.Lists["NameOfYourList"];                    dropSite.DataSource = list.Items;                    dropSite.DataValueField = "Title"; dropSite.DataTextField = "Title";                    dropSite.DataBind();                }            }        }

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

  • Anonymous
    January 26, 2011
    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

  • Anonymous
    May 26, 2011
    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

  • Anonymous
    July 28, 2013
    hi, how can i bind a menu control to a sharepoint list?? anyone any idea please share asap

  • Anonymous
    October 29, 2013
    The comment has been removed

  • Anonymous
    August 04, 2014
    cboDepartment.DataValueField="text "+","+"text "; i want to achieve this how to do it. plz help me out

  • Anonymous
    October 12, 2014
    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?