In this article we can see how to create a new Visual Web Part for SharePoint 2013 with Visual Studio 2012.
Follow the bellow steps to create a new Visual Web Part
1. Create a new Empty SharePoint 2013 Project.
1. Create a new Empty SharePoint 2013 Project.
2. Select Solution as farm solution.
4. Now solution will open in visual studio.Add three labels in .ascx file
<asp:Label ID="lblTitle" runat="server" Text="Label"></asp:Label>
<asp:Label ID="lblFirstName" runat="server" Text="Label"></asp:Label>
<asp:Label ID="lblLastName" runat="server" Text="Label"></asp:Label>
<asp:Label ID="lblFirstName" runat="server" Text="Label"></asp:Label>
<asp:Label ID="lblLastName" runat="server" Text="Label"></asp:Label>
5. Write below code in page load in ascx.cs file to access SharePoint list data in Web part.
string strUrl = "http://spdev-3:1001";
using (SPSite oSite = new SPSite(strUrl))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
SPList list = oWeb.Lists["TestList"];
SPQuery query = new SPQuery();
query.Query = "<Where><Eq> <FieldRef Name='Title' /><Value Type='Text'>PERM</Value></Eq></Where>";
query.RowLimit = 1;
SPListItemCollection spItemCollection = list.GetItems(query);
SPListItem spItem=spItemCollection[0];
lblTitle.Text = spItem["Title"]!=null?spItem["Title"].ToString():string.Empty;
lblFirstName.Text = spItem["FirstName"] != null ? spItem["FirstName"].ToString() : string.Empty;
lblLastName.Text = spItem["LastName"] != null ? spItem["LastName"].ToString() : string.Empty;
}
}
6. Right click on solution and deploy web part.
7. Add web part in SharePoint Page.After adding web part on page the data will show on a page.
8. Now you able to access SharePoint List data in Web part.
using (SPSite oSite = new SPSite(strUrl))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
SPList list = oWeb.Lists["TestList"];
SPQuery query = new SPQuery();
query.Query = "<Where><Eq> <FieldRef Name='Title' /><Value Type='Text'>PERM</Value></Eq></Where>";
query.RowLimit = 1;
SPListItemCollection spItemCollection = list.GetItems(query);
SPListItem spItem=spItemCollection[0];
lblTitle.Text = spItem["Title"]!=null?spItem["Title"].ToString():string.Empty;
lblFirstName.Text = spItem["FirstName"] != null ? spItem["FirstName"].ToString() : string.Empty;
lblLastName.Text = spItem["LastName"] != null ? spItem["LastName"].ToString() : string.Empty;
}
}
6. Right click on solution and deploy web part.
7. Add web part in SharePoint Page.After adding web part on page the data will show on a page.
8. Now you able to access SharePoint List data in Web part.
0 comments:
Post a Comment