SharePoint 2013 - SPLessons

Chapter 29

SharePoint List Data

SharePoint List Data

Get The list items from the sharepoint list, Here am getting data from the Splessons List , and then data was binding into the the Dropdown.

private void GetListItems()
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
                    {
                        using (SPWeb objWeb = oSite.OpenWeb())
                        {
                            SPList oList = objWeb.Lists["Splessons"];
                            SPListItemCollection oListCollection = oList.GetItems();

                            ddlLocation.DataSource = oListCollection.GetDataTable();
                            ddlLocation.DataValueField = "ID";
                            ddlLocation.DataTextField = "Location";
                            ddlLocation.DataBind();                           

                        }
                    }
                });
            }
            catch (Exception ex)
            {
                Page.ClientScript.RegisterClientScriptBlock(typeof(SPAlert), "alert", "<script language="\&quot;javascript\&quot;"></script>"); 
            } 
     }