Wednesday, April 13, 2011

Web part to show data from list

Requirement is to show news on web page, data saved in custom list . A webpart to get data from list and show on page:

1. Open Visual Studio and create a class file or webpart (if sharepoint extension is there with VS.
2. Code of ShowNews.cs file below:


using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Drawing;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace MyWebParts
{
[Guid("6e1a7cf2-bf98-4926-81c9-0c0184ba6a60")]
public class ShowNews : System.Web.UI.WebControls.WebParts.WebPart
{

//Controls
private Label lblTitle;
private Label lblNews;

// A table that is used to layout the controls
private Table tbMain;
private TableRow tRow;
private TableCell tCell;

#region "Web Part Properties"

private const string defaultSiteCollection = "NewTeamSite";
private string _siteName = defaultSiteCollection;
[Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
WebDisplayName("Site Name"),
WebDescription("Site from where to fetch News")]

public string SiteName
{
get { return this._siteName; }
set { this._siteName = value; }
}

private const string defaultList = "News";
private string _listName = defaultList;
[Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
WebDisplayName("List Name"),
WebDescription("List from where to fetch News")]

public string ListName
{
get { return this._listName; }
set { this._listName = value; }
}
#endregion

public ShowNews()
{
}

protected override void CreateChildControls()
{
base.CreateChildControls();

// TODO: add custom rendering code here.
tbMain = new Table();
tbMain.BorderStyle = BorderStyle.Double;
tbMain.BorderColor = Color.Silver;
tbMain.BackColor = Color.WhiteSmoke;


lblTitle = new Label();
lblTitle.Text = "News";
lblTitle.Font.Bold = true;

tRow = new TableRow();
tCell = new TableCell();
tCell.VerticalAlign = VerticalAlign.Top;
tCell.HorizontalAlign = HorizontalAlign.Center;
tCell.Controls.Add(lblTitle);
tRow.Controls.Add(tCell);
tbMain.Controls.Add(tRow);


lblNews = new Label();
lblNews.Text = "Blank";

tRow = new TableRow();
tCell = new TableCell();
tCell.VerticalAlign = VerticalAlign.Top;
tCell.HorizontalAlign = HorizontalAlign.Left;
tCell.Controls.Add(lblNews);
tRow.Controls.Add(tCell);
tbMain.Controls.Add(tRow);

this.Controls.Add(tbMain);

CreateNewsHtml();
}

private void CreateNewsHtml()
{
SPList newsList;
SPListItemCollection listitemcoll;

try
{
using (SPSite site = new SPSite(this.Context.Request.Url.ToString()))
{
using (SPWeb web = site.AllWebs[this._siteName])
{
newsList =web.Lists[this._listName];
listitemcoll = newsList.Items;
if (newsList.ItemCount > 0)
foreach(SPListItem item in listitemcoll)
{
lblTitle.Text = item["Title"].ToString();
lblNews.Text = item["News"].ToString();
}

}
}
}
catch (Exception ex)
{

}
}
}
}


3. Create ShowNews.webpart file (get created itself with Sharepoint extension)

<?xml version="1.0" encoding="utf-8"?>
<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<type name="MyWebparts.ShowNews, ShowNews, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXXXXXXXXXXXXXXX" />
<importErrorMessage>Cannot import ShowNews Web Part.</importErrorMessage>
</metaData>
<data>
<properties>
<property name="Title" type="string">ShowNews Web Part</property>
<property name="Description" type="string">Show News from List on Page</property>
<property name="AllowZoneChange" type="bool">True</property>
<property name="AllowHide" type="bool">True</property>
</properties>
</data>
</webPart>
</webParts>


4. Now, your webpart is ready, deploy it either [1] manually; GAC the DLL, upload webpart file to webpart gallery and made safecontrol entry for webpart in web.config file. or [2] create a deployment package to deploy the webpart.

Happy Coding!!!

No comments:

Post a Comment