24-9-2010 22:27:31
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace GetCustomerWP
{
[Guid("8e91c8ff-be34-4ba3-8b1b-8a80e2128d9b")]
public class GetCustomerWP : System.Web.UI.WebControls.WebParts.WebPart
{
GridView gvCustomer;
public GetCustomerWP()
{
this.ExportMode = WebPartExportMode.All;
}
protected override void CreateChildControls()
{
base.CreateChildControls();
DataTable table = new DataTable("Customers");
table.Columns.Add("TenKhach", typeof(string));
table.Columns.Add("Amount", typeof(int));
using (SPWeb oWeb = SPContext.Current.Web)
{
SPList oList = oWeb.Lists["Customers"];
SPListItemCollection oItems = oList.Items;
foreach (SPListItem item in oItems)
{
DataRow row = table.NewRow();
row["TenKhach"] = item["Title"].ToString();
row["Amount"] = item["Amount"].ToString();
table.Rows.Add(row);
}
}
gvCustomer = new GridView();
gvCustomer.DataSource = table;
gvCustomer.DataBind();
this.Controls.Add(gvCustomer);
}
}
}
Cái này để truy cập vào list có tên là customers
Các bạn có thể dùng CAML để truy xuất dữ liệu có điều kiện trong list Pages của SharePoint. Có thể dùng cái này để làm danh sách bài viết trong SharePoint Portal