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

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

Also you can check out:

Assign Manager to user from active directory to SharePoint 2010

How to set formula for the calculated field in a list using the Client Side Object Model in Auto hosted apps?

Remove Extended Zone using PowerShell in SharePoint 2013

1. First we create visual studio web part under Empty SharePoint project. See in fig

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

2. Now we add class under 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;
}
}

}
}

3. 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>

4. 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();
}
}

}
}

5. 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);
}
}
}

6. Now if you go in correct way than you will find under web part properties your custom properties and see all lists in dropdown list. See in fig.

Show all list in dropdown list using visual web part configuration properties in SharePoint 2016
Show all list in the dropdown list using visual web part configuration properties in SharePoint 2016

7. Now select the list and click on OK or apply.

8. Now you see the selected list as a grid view. See fig.

Show all list in dropdown list using visual web part configuration properties in SharePoint 2013
Show all list in the dropdown list using visual web part configuration properties in SharePoint 2013

You May Also like the Following SharePoint Online Tutorials:

About Navneet Singh

I am Navneet Singh, Working at C3it software solutions pvt. ltd. in Pune. My core skills are SharePoint 2013, Office 365, SharePoint 2010, SharePoint Designer, SharePoint Designer Workflow, Nintex Workflow, Infopath, Webpart, CSOM/SSOM, Power-shell, C#, ASP.NET and SQL Server.

View all posts by Navneet Singh →

Leave a Reply