OFC204 - Sample Code and Demos I showed at TechEd Last Week
At my TechEd talk last week I showed Visual Studio 2008 extensions for SharePoint 1.3 and I demo'd creating a simple web part that shows data from a SharePoint list in a grid. I also showed command line builds which is one of the features we added in VSeWSS 1.3 and I showed the SPDisposeCheck tool.
For the demos I used the WSS Developer VPC which is available here. I uninstalled the VSeWSS 1.2 that comes on that image and installed the VSeWSS 1.3 from here. I also installed SPDisposeCheck from here.
So that's my machine image to which I also created a sample Employees list and a Projects list in the default SharePoint site. Here's the code that I used in the first demo. Pretty simple stuff.
namespace WebPart1
{
[Guid("64a11214-36e3-4b1a-b8a7-fbb0ca9370c6")]
public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
{
public WebPart1()
{
}
protected override void CreateChildControls()
{
SPGridView customerGridView = new SPGridView();
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["Employees"];
SPQuery query = new SPQuery(list.DefaultView);
query.Query = "<Where><Eq><FieldRef Name='JobTitle' /><Value Type='Text'>SDE</Value></Eq></Where>";
SPListItemCollection items = list.GetItems(query);
AutoAddColumns(customerGridView, list);
customerGridView.DataSource = items.GetDataTable();
customerGridView.DataBind();
Controls.Add(customerGridView);
base.CreateChildControls();
}
private void AutoAddColumns(SPGridView gridView, SPList list)
{
gridView.AutoGenerateColumns = false;
foreach (string fieldname in list.DefaultView.ViewFields)
{
SPField field = list.Fields.GetFieldByInternalName(fieldname);
BoundField column = new BoundField();
column.DataField = field.StaticName;
column.HeaderText = field.Title;
gridView.Columns.Add(column);
}
}
}
}