Populate ASP.net DropDown from SharePoint Lists
A simple scenario I had this morning, was to populate ASP.net Web page’s drop downs from a SharePoint list.
A fairly simple task using Microsoft.SharePointOnline.CSOM, until I was faced with the list threshold (which I will cover in another post).
Now, in my aspx page, I had to:
1- Reference the required name spaces
using System.Web.UI.WebControls; using Microsoft.SharePoint.Client;
2- The method to load the DropDowns
private void LoadData() { try { string WebUrl = "https://******.sharepoint.com/sites/****/****/"; string login = "samir.daoudi@**********"; string password = "**********";"; var securePassword = new SecureString(); foreach (char c in password) { securePassword.AppendChar(c); } var onlineCredentials = new SharePointOnlineCredentials(login, securePassword); using (ClientContext CContext = new ClientContext(WebUrl)) { CContext.Credentials = onlineCredentials; List ShpList = CContext.Web.Lists.GetByTitle("YOURLISTNAME"); CamlQuery CamlQ = CamlQuery.CreateAllItemsQuery(1000); Microsoft.SharePoint.Client.ListItemCollection items = ShpList.GetItems(CamlQ); CContext.Load(items); CContext.ExecuteQuery(); C_Company.Items.Clear(); foreach (Microsoft.SharePoint.Client.ListItem I in items) { C_Company.Items.Add(new System.Web.UI.WebControls.ListItem() { Text = I["Title"].ToString(), Value = I["ID"].ToString() }); } } } catch (Exception exp) { // Do something with the exception } }