In this article I am going to show how we
can create a Visual Web Part and how we can deploy web part.
In this web part I am showing a drop down
list in which I am binding All SharePoint List and on selecting a particular
list I am showing all items of this selected list in a GridView.
We will learn
it step by step…
Open Visual Studio and select Blank
SharePoint Project..

Image 1.
Give your SharePoint site URL and
validate…

Image 2.
Now Right Click on Solution Explorer and
add a Visual Web part…

Image 3.
Now
design your web part.. my ascx is:
<%@AssemblyName="$SharePoint.Project.AssemblyFullName$"%>
<%@AssemblyName="Microsoft.Web.CommandUI, Version=14.0.0.0,
Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>
<%@RegisterTagPrefix="SharePoint"Namespace="Microsoft.SharePoint.WebControls"
Assembly="Microsoft.SharePoint,
Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>
<%@RegisterTagPrefix="Utilities"Namespace="Microsoft.SharePoint.Utilities"Assembly="Microsoft.SharePoint, Version=14.0.0.0,
Culture=neutral,
PublicKeyToken=71e9bce111e9429c"%>
<%@RegisterTagPrefix="asp"Namespace="System.Web.UI"Assembly="System.Web.Extensions, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"%>
<%@ImportNamespace="Microsoft.SharePoint"%>
<%@RegisterTagPrefix="WebPartPages"Namespace="Microsoft.SharePoint.WebPartPages"
Assembly="Microsoft.SharePoint,
Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>
<%@ControlLanguage="C#"AutoEventWireup="true"CodeBehind="ShowListUserControl.ascx.cs"
Inherits="WVP_ShowListItem.ShowList.ShowListUserControl"%>
<tablecellpadding="4"cellpadding="4"width="90%"align="center"style="border: solid 1px Gray;">
<tr>
<td>
<asp:LabelID="lblErrorMsg"ForeColor="Red"runat="server"></asp:Label><br/>
<asp:LabelID="lblSelectList"runat="server"Text="Select
Your List"></asp:Label>
<asp:DropDownListID="ddlListCollection"runat="server"OnSelectedIndexChanged="ddlListCollection_SelectedIndexChanged"
AutoPostBack="true">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:GridViewID="grdListItem"runat="server"EmptyDataText="There
is no item."EmptyDataRowStyle-ForeColor="Red">
</asp:GridView>
</td>
</tr>
</table>
My
ascx.cs is:
using System;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using System.Data;
using
System.Data.SqlClient;
using
Microsoft.SharePoint;
namespace
WVP_ShowListItem.ShowList
{
public partial class ShowListUserControl : UserControl
{
protected void
Page_Load(object sender, EventArgs e)
{
if
(!Page.IsPostBack)
BindAllListName();
}
private void
BindAllListName()
{
try
{
DataTable dt = newDataTable();
dt.Columns.Add("TitleName", typeof(string));
using (SPSite site = newSPSite("http://localhost:7000"))
{
using (SPWeb web = site.OpenWeb())
{
foreach (SPList oList in
web.Lists)
{
DataRow dr =
dt.NewRow();
dr["TitleName"]
= oList.Title.ToString();
dt.Rows.Add(dr);
}
ddlListCollection.DataSource = dt;
ddlListCollection.DataValueField = "TitleName";
ddlListCollection.DataTextField
= "TitleName";
ddlListCollection.DataBind();
}
}
}
catch (SPException exp)
{
lblErrorMsg.Text = exp.Message.ToString();
}
}
protected void
ddlListCollection_SelectedIndexChanged(object
sender, EventArgs e)
{
using (SPSite site = newSPSite("http://localhost:7000"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list =
web.Lists[ddlListCollection.SelectedValue.ToString()];
SPListItemCollection items = list.Items;
if
(items.GetDataTable().Rows.Count > 0)
{
grdListItem.DataSource = items.GetDataTable();
grdListItem.DataBind();
}
else
{
grdListItem.DataSource = null;
grdListItem.DataBind();
}
}
}
}
}
}
Now Right Click on Solution Explorer and
Click on Deploy
After successfully deployed Open your Site
and Select page where you want to add this web part. In this solution I am
going to use this web part on MyTest2.aspx page so go to page and click on
Edit:
Click on insert -> Select Web Part ->
Select Custom -> Select Your web part -> Click Add.

Image 4.
Save & Close.
Now open you page: All list will display in
drop down

Image 5.
Select A List…

Image 6.
Now select any list and see all items of
selected List.