WebPart 对于学习和研究SharePoint的人,已经不是什么新鲜的概念了。但对于学习.net Framework的人,可能会发现在System.web.ui.webcontrols下会发现多了WebPart的踪影。WebPart作为 WebControl中的一种,比WebControl更高级一些,它可以在线Drag and Drop,并可以在线设置它的属性。 以前利用 VS2003开发WebPart,得安装WebPart Template For VSNet。当然也可以在VS2005下安装WebPart Template来开发WebPart。在这里我就不介绍用WebPart Template For VSNet来开发WebPart了。 在VS2005下就可以不用安装WebPart Template来开发WebPart。下面我具体介绍其过程: 1、创建 Web Control Library 首先浏览C# Project Templates,然后选择Web Control Library,输入“SampleControl”。 要开发WebPart用于SharePoint,就必须引用Microsoft.SharePoint.dll(必须是安装MOSS的服务器上的)。 最后添加引用如下图: 2、编写WebPart的代码 你开发的WebPart,根据自己开发的功能选择相应的SP(SharePoint)命名空间,具体命名空间,请参考SDK。 开发WebPart必须继承WebPart类。
//-------------------------------------------------------------------- // File: SimpleWebPart.cs //// Purpose: A sample Web Part that demonstrates how to create a basic // Web Part. //--------------------------------------------------------------------using System; using System.ComponentModel; using System.Runtime.InteropServices; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml.Serialization; using Microsoft.SharePoint; using Microsoft.SharePoint.WebPartPages; using Microsoft.SharePoint.Utilities; using System.Web.UI.HtmlControls; namespace Xdian.WebParts.SampleControl { /**////<summary>/// This Web Part changes it's own title and implements a custom property. ///</summary> [XmlRoot(Namespace ="MyWebParts")] publicclass SimpleWebPart : WebPart { privateconststring defaultText ="hello"; privatestring text = defaultText; // Declare variables for HtmlControls user interface elements. HtmlButton _mybutton; HtmlInputText _mytextbox; // Event handler for _mybutton control that sets the // Title property to the value in _mytextbox control.publicvoid _mybutton_click(object sender, EventArgs e) { this.Title = _mytextbox.Value; try{ this.SaveProperties =true; }catch{ Caption ="Error Could not save property."; } }// Override the ASP.NET Web.UI.Controls.CreateChildControls // method to create the objects for the Web Part's controls. protectedoverridevoid CreateChildControls() { // Create _mytextbox control. _mytextbox =new HtmlInputText(); _mytextbox.Value =""; Controls.Add(_mytextbox); // Create _mybutton control and wire its event handler. _mybutton =new HtmlButton(); _mybutton.InnerText ="Set Web Part Title"; _mybutton.ServerClick +=new EventHandler(_mybutton_click); Controls.Add(_mybutton); } [Browsable(true), Category("Miscellaneous"), DefaultValue(defaultText), WebPartStorage(Storage.Personal), FriendlyName("Text"), Description("Text Property")] publicstring Text { get{ return text; }set{ text = value; } }protectedoverridevoid RenderWebPart(HtmlTextWriter output) { RenderChildren(output); // Securely write out HTML output.Write("<BR>Text Property: "+ SPEncode.HtmlEncode(Text)); } }}
<?xml version="1.0"?><WebPart xmlns="http://schemas.microsoft.com/WebPart/v2"><Assembly>SampleControl, Version=1.0.0.0, Culture=neutral,PublicKeyToken=0e79ac0ff7e9e2cb</Assembly><TypeName>Xdian.WebParts.SampleControl.SimpleWebPart</TypeName><Title>SampleWebPart</Title><Description>Chatterley Create The first WebPart</Description></WebPart>