Sunday, August 21, 2011

Retrieve data form SharePoint list and populate in asp.net DropdownList

If you need populate data form a list and show in a dropdown list, it easy when you create a String type list then it can assign to Dropdown list’s “Data Source” below code illustrate how it can done

        private List LoadDropDownList(SPList spList)
        {
            SPQuery  quary = new SPQuery();
            SPListItemCollection titleItems = spList.GetItems(quary);

            List howList = new List();

            if (titleItems.Count>0)
            {
                foreach (SPListItem titleitem in titleItems)
                {
                    howList.Add(titleitem["Title"].ToString());
                }
            }
            else
            {
                howList.Add("No records to load");
            }

            return howList;            
        }

Then you can easyly assign that string type list to Dropdown “Data Source” as follows

howDropDownList.DataSource = LoadDropDownList(findOutlist);
howDropDownList.DataBind();

1 comment: