Show all list in a dropdown list using visual web part configuration properties in SharePoint 2010

In this SharePoint tutorial, we will discuss how to show all list in a dropdown list using visual web part configuration properties in SharePoint 2010.

First, we create a visual studio web part under the Empty SharePoint project. See in fig

sharepoint 2010 visual web part

Now we add class under the web part and replace the following code under this class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI;
using Microsoft.SharePoint;

namespace LeaveApplication.LeaveWebPart
{
class WPEditor : EditorPart
{
private DropDownList ddlList;
private Label lbl;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);

ddlList = new DropDownList();
lbl = new Label();

this.lbl.ID = "lbl";
this.lbl.Text = "Select Holiday List";

SPWeb oWeb = SPContext.Current.Web;
foreach (SPList oLists in oWeb.Lists)
{
ddlList.Items.Add(oLists.Title);
}
}

protected override void CreateChildControls()
{
base.CreateChildControls();
this.Controls.Add(lbl);
this.Controls.Add(ddlList);
this.Controls.Add(new LiteralControl("<br/>"));
}

//ApplyChanges is used to save any changes to the currently-edited Web Part.
public override bool ApplyChanges()
{
LeaveWebPart webPart = this.WebPartToEdit as LeaveWebPart;

if (webPart!= null)
{
webPart.HolidayName = ddlList.SelectedValue.Trim().ToString();
}
return true;
}

// SyncChanges is used to load the current configuration from Web Part Properties.
public override void SyncChanges()
{
LeaveWebPart webPart = this.WebPartToEdit as LeaveWebPart;
if (webPart != null)
{
ddlList.SelectedValue = webPart.HolidayName;
}
}
}
}

We add grid view control under .ascx file. (For showing selected list in grid view)

<asp:Label ID="lbal1″ runat="server"/>
<asp:GridView ID="grdforlist" runat="server"></asp:GridView>

Now we add the following code under .ascx.cs file for showing the selected list under grid view control.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace LeaveApplication.LeaveWebPart
{
public partial class LeaveWebPartUserControl : UserControl
{
public LeaveWebPart lwebpart{ get; set; }
protected void Page_Load(object sender, EventArgs e)
{

}

protected override void OnPreRender(EventArgs e)
{
try
{
base.OnPreRender(e);

if (this.lwebpart.HolidayName != null)
{
SPWeb oWeb = SPControl.GetContextWeb(HttpContext.Current);
SPList oList = oWeb.Lists[this.lwebpart.HolidayName];
SPListItemCollection oColl = oList.Items;
grdforlist.DataSource = oColl.GetDataTable();
grdforlist.DataBind();
}
}
catch (Exception ex)
{
lbal1.Text = ex.ToString();
}
}
}
}

Now add the following code under webpart.cs file.

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace LeaveApplication.LeaveWebPart
{
[ToolboxItemAttribute(false)]
public class LeaveWebPart : WebPart,IWebEditable
{
public string HolidayName { get; set; }
public string WeekendName { get; set; }

// Visual Studio might automatically update this path when you change the Visual Web Part project item.
private const string _ascxPath = @"~/_CONTROLTEMPLATES/LeaveApplication/LeaveWebPart/LeaveWebPartUserControl.ascx";

protected override void CreateChildControls()
{
try
{
LeaveWebPartUserControl control = Page.LoadControl(_ascxPath) as LeaveWebPartUserControl;

if (control != null)
{
control.lwebpart = this;
}
Controls.Add(control);
}catch(Exception ex){
ex.ToString();
}
}

public override EditorPartCollection CreateEditorParts()
{
List<EditorPart> editorpart = new List<EditorPart>();
WPEditor wpobject = new WPEditor();
wpobject.ID = this.ID + "_sampleEditorPart";
wpobject.Title = "Custom";
editorpart.Add(wpobject);
return new EditorPartCollection(base.CreateEditorParts(), editorpart);
}
}
}

Now if you go incorrect way then you will find under web part properties your custom properties and see all lists in the dropdown list. See in fig.

sharepoint 2010 visual web part properties

Now select the list and click on OK or apply. Now you see the selected list as a grid view. See fig.

sharepoint 2010 visual web part dropdown list properties

You may like following SharePoint visual web part tutorials:

Here we learned how to show all lists in a dropdown list using visual web part configuration properties in SharePoint 2010.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>