Search This Blog

Monday, March 2, 2015

Filling Drop down from list in sharepoint 2010

i am binding Country dropdown from the sharepoint list named "Country".
Country list have OOTB column named ID , and Title ,
In Title column i am storing the Name of the Country,
my .ascx code
<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true"></asp:DropDownList>
And my code behind code
public void BindCountryDropDown()
  {
   SPQuery query = new SPQuery();
   SPList listCountry = SPContext.Current.Web.Lists.TryGetList("Country");
   if (listCountry != null)
   {
    DataTable dtCountry = new DataTable();
    dtCountry = listCountry.GetItems(query).GetDataTable();
    ddlCountry.DataSource = dtCountry;
    ddlCountry.DataValueField = "ID";
    ddlCountry.DataTextField = "Title";
    ddlCountry.DataBind();
    ddlCountry.Items.Insert(0, "--Select Country--");
   }
  }
 Another Exapmle
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace MyVisualWebPartProject.VisualWebPart
{
    class HtmlEditor : EditorPart
    {
        #region Private Member Variables
        private Panel pnlHtmlEditor;
        private Panel pnlMain;
        private Literal literal;
        private TextBox tbSiteUrl;
        private DropDownList ddlLists;
        private DropDownList ddlViews;
        #endregion

        #region Constructors
        public HtmlEditor()
        {
            this.ID = "HtmlEditor";
        }
        #endregion

        #region Private Methods
        ///
        /// This method will populate the Lists DropDownList with the names of all the lists from the given Site URL
        /// Filtering to only show GenerLists
        ///

        private void InitializeFilteredListDropDown()
        {
            this.ddlLists.Items.Clear();
            ListItem selectItem = new ListItem(" - Select List - ", string.Empty);
            this.ddlLists.Items.Add(selectItem);

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                string siteUrl = string.IsNullOrEmpty(this.tbSiteUrl.Text) ? SPContext.Current.Web.Url : SPContext.Current.Site.Url + this.tbSiteUrl.Text;

                try
                {
                    using (SPSite site = new SPSite(siteUrl))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPListCollection allLists = web.Lists;

                            foreach (SPList list in allLists)
                            {
                                if (list.BaseTemplate == SPListTemplateType.GenericList)
                                {
                                    ListItem item = new ListItem();
                                    item.Text = list.Title;
                                    item.Value = list.Title;

                                    this.ddlLists.Items.Add(item);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("MyVisualWebPart", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
                }
            });
         }

        ///
        /// This method will populate the Views DropDownList with the names of all the views from the current selected List
        ///

        private void InitializeViewsDropdown()
        {
            this.ddlViews.Items.Clear();
            ListItem selectItem = new ListItem(" - Select View - ", string.Empty);
            this.ddlViews.Items.Add(selectItem);

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                string siteUrl = string.IsNullOrEmpty(this.tbSiteUrl.Text) ? SPContext.Current.Web.Url : SPContext.Current.Site.Url + this.tbSiteUrl.Text;

                try
                {
                    using (SPSite site = new SPSite(siteUrl))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList list = web.Lists.TryGetList(this.ddlLists.SelectedValue);

                            if (list != null)
                            {
                                foreach (SPView view in list.Views)
                                {
                                    if (!view.Hidden)
                                    {
                                        ListItem item = new ListItem();
                                        item.Text = view.Title;
                                        item.Value = view.Title;

                                        this.ddlViews.Items.Add(item);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("MyVisualWebPart", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
                }
            });
        }

        ///
        /// This event will re-populate the Views DropDownList with all the views for the current selected List
        ///

        ///
        ///
        private void ListSelectedIndexChanged(object sender, EventArgs e)
        {
            InitializeViewsDropdown();
            this.ddlViews.SelectedValue = string.Empty;
        }

        ///
        /// This event will re-populate the Lists DropDownList with all the lists for the current Site URL
        ///

        ///
        ///
        private void SiteUrlTextChanged(object sender, EventArgs e)
        {
            InitializeFilteredListDropDown();
            this.ddlLists.SelectedValue = string.Empty;
        }
        #endregion

        #region Public Methods
        ///
        /// Creates all the necessary controls for the Custom Web Part Editor
        ///

        protected override void CreateChildControls()
        {
            this.Title = "My Visual WebPart Properties";

            Controls.Clear();

            this.pnlHtmlEditor = new Panel();
            this.pnlHtmlEditor.CssClass = "ms-ToolPartSpacing";
            this.Controls.Add(pnlHtmlEditor);

            this.pnlMain = new Panel();

            this.literal = new Literal();
            this.literal.Text = "</pre>
<div class="UserSectionHead">Site URL</div>
<div class="UserSectionBody">
<div class="UserControlGroup">";
 this.pnlMain.Controls.Add(this.literal);
 this.tbSiteUrl = new TextBox();
 this.tbSiteUrl.AutoPostBack = true;
 this.tbSiteUrl.TextChanged += SiteUrlTextChanged;
 this.pnlMain.Controls.Add(this.tbSiteUrl);
 this.literal = new Literal();
 this.literal.Text = "</div>
</div>
<pre>
";
            this.pnlMain.Controls.Add(literal);

            this.ddlLists = new DropDownList();
            this.ddlLists.AutoPostBack = true;
            this.ddlLists.SelectedIndexChanged += ListSelectedIndexChanged;
            this.ddlLists.CssClass = "UserInput";
            this.ddlLists.Width = new Unit("175px", CultureInfo.InvariantCulture);
            InitializeFilteredListDropDown();
            this.literal = new Literal();
            this.literal.Text = "</pre>
<div class="UserSectionHead">List</div>
<div class="UserSectionBody">
<div class="UserControlGroup">";
 this.pnlMain.Controls.Add(this.literal);
 this.pnlMain.Controls.Add(this.ddlLists);
 this.literal = new Literal();
 this.literal.Text = "</div>
</div>
<pre>
";
            this.pnlMain.Controls.Add(literal);

            this.ddlViews = new DropDownList();
            this.ddlViews.CssClass = "UserInput";
            this.ddlViews.Width = new Unit("175px", CultureInfo.InvariantCulture);
            InitializeViewsDropdown();
            this.literal = new Literal();
            this.literal.Text = "</pre>
<div class="UserSectionHead">View</div>
<div class="UserSectionBody">
<div class="UserControlGroup">";
 this.pnlMain.Controls.Add(this.literal);
 this.pnlMain.Controls.Add(this.ddlViews);
 this.literal = new Literal();
 this.literal.Text = "</div>
</div>
<pre>
";
            this.pnlMain.Controls.Add(literal);

            pnlHtmlEditor.Controls.Add(pnlMain);
        }

        ///
/// Renders the Web Part Editor Part HTML
        ///
        ///
        protected override void RenderContents(HtmlTextWriter writer)
        {
            pnlMain.RenderControl(writer);
        }

        ///
/// Applies the changes to the Photo Gallery Web Part
        ///
        /// Returns true if the changes applied
        public override bool ApplyChanges()
        {
            EnsureChildControls();
            MyVisualWebPart myVisualWebPart = WebPartToEdit as MyVisualWebPart;

            if (myVisualWebPart == null)
            {
                return false;
            }

            myVisualWebPart.SiteUrl = this.tbSiteUrl.Text;
            myVisualWebPart.List = this.ddlLists.SelectedValue;
            myVisualWebPart.View = this.ddlViews.SelectedValue;

            return true;
        }

        ///
/// Syncs the values from the Photo Gallery to the Web Part Editor
        ///
        public override void SyncChanges()
        {
            EnsureChildControls();
            MyVisualWebPart myVisualWebPart = WebPartToEdit as MyVisualWebPart;

            if (myVisualWebPart == null)
            {
                return;
            }

            this.tbSiteUrl.Text = myVisualWebPart.SiteUrl;
            InitializeFilteredListDropDown();
            this.ddlLists.SelectedValue = myVisualWebPart.List;
            InitializeViewsDropdown();
            this.ddlViews.SelectedValue = myVisualWebPart.View;
        }
        #endregion
    }
}


No comments:

Post a Comment