One of the few things we are still missing in Visual Studio 2005 is the ability to visually design webparts for SharePoint 2007. In ASP.Net 2.0 you can use a web user control (ascx) as web part when it implements the IWebPart interface because of the ASP.Net 2.0 GenericWebPart class . The GenericWebPart control exists to provide a run-time wrapper for server controls that are not WebPart controls, so that such controls can be used in Web Parts pages and applications. Unfortunately, this GenericWebPart is not available SharePoint 2007 - so you have to build your own wrapper - but this is not very hard as I will show in this posting.
First create an empty web site, afterwards add one user control wpgrid.ascx To make things easier, uncheck the "Place code in a separate file" when adding the user control. Switch to design view and add a GridView Control - let's call it gvDemo, set autogeneratecolumns to false and add one bound column (Datafield=Title). Now create a directory usercontrols at the root of your SharePoint site - for my default SharePoint 2007 site, this is C:\Inetpub\wwwroot\wss\VirtualDirectories\80. Copy the wpgrid.ascx file to this location.
Now let's create a new web part - as you probably already know, you can now uses ASP.Net 2.0 webparts in SharePoint 2007 - if you don't have any experience with web parts - check out Marts - Create a SharePoint 2007 web part step by step. The web part will show the title of all items in a list in the grid with paging enabled - so first create a web part property with the name of list you want to show.
public class UCWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
private UserControl usercontrol;
private GridView gvDemo;
private const string defaultlist = "";
private string _listtolink = defaultlist;
protected DataTable dtDemo = null;
[Personalizable(), WebBrowsable(),
WebDisplayName("List to display"),
WebDescription("Name of the list in this site to display")]
public string ListToLink
{
get { return _listtolink; }
set { _listtolink = value; }
}
Override the CreateChildControls method, where you will load the user control, set the necessary properties and create an event handler to implement the paging on the gridview.
protected override void CreateChildControls()
{
try
{
base.CreateChildControls();
this.Controls.Clear();
this.GetData();
usercontrol = (UserControl)Page.LoadControl(@"/usercontrols/wpgrid.ascx");
gvDemo = (GridView)this.usercontrol.FindControl("gvDemo");
gvDemo.AllowPaging = true;
gvDemo.DataSource = dtDemo;
gvDemo.PageSize = 3;
gvDemo.PageIndexChanging += new GridViewPageEventHandler(gvDemo_PageIndexChanging);
this.Controls.Add(usercontrol);
gvDemo.DataBind();
}
catch (Exception ex)
{
EventLog.WriteEntry("WebParts", "UCWebPart" + ex.ToString());
}
}
void gvDemo_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvDemo.PageIndex = e.NewPageIndex;
gvDemo.DataBind();
}
The GetData() method loads the DataTable with the title of the different items in the list:
private void GetData()
{
try
{
if (ListToLink.Length > 0)
{
dtDemo = new DataTable();
dtDemo.Columns.Add("Title", Type.GetType("System.String"));
SPWeb site = SPContext.Current.Web;
SPList list = site.Lists[_listtolink];
foreach (SPListItem item in list.Items)
{
DataRow newRow = dtDemo.NewRow();
newRow["Title"] = item["Title"];
dtDemo.Rows.Add(newRow);
}
}
}
catch (Exception ex)
{
EventLog.WriteEntry("WebParts", "UCWebPart - Retrieving items from " + _listtolink + "-" + ex.ToString(), EventLogEntryType.Error);
}
}
PS. You can also use the SmartPart v2 (aka SonOfSmartPart) to load ASP.Net 2.0 user controls in SharePoint 2007 web parts ... but as you have seen above, it is quite easy to do it yourself...
Tags: sharepoint2007, sharepoint+2007, moss, moss2007, wss30, aspnet, webpart
Currently listening : Adema - Unstable Disc 1