在MOSS中开发WebPart比在SPS中开发方便了好多,不用再去管什么Manifest.xml、webconfig等,只要编写完WebPart后直接部署就可以将WebPart部署到站点上,确实省了我们好多功夫。只要到微软网站上下载一个叫VSeWSS.msi的安装包,安装后就可以在VS.NET 2005上创建WebPart了。下面的WebPart显示站点中某一列表下某一字段的内容
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 Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace Web_Part
{
[Guid("f7f040ac-6db7-489c-b939-3330a7f7f406")]
public class Web_Part : System.Web.UI.WebControls.WebParts.WebPart
{
public Web_Part()
{
this.ExportMode = WebPartExportMode.All;
}
TextBox textBox;
SPWeb spWeb;
protected override void CreateChildControls()
{
textBox = new TextBox();
SPSite site = SPControl.GetContextSite(Context);
foreach (SPWeb web in site.AllWebs)
{
if (web.IsRootWeb)
{
spWeb = web; break;
}
}
SPList list = spWeb.Lists["任务"];
textBox.TextMode = TextBoxMode.MultiLine;
foreach (SPListItem item in list.Items)
{
textBox.Text = textBox.Text + item["标题"].ToString();
}
Controls.Add(textBox);
}
}
}