使用Visual Studio.NET 开发Web Parts(Web部件)
对SharePoint进行扩充的一个最好的方法就是开发自己的Web部件,但是在开发Web部件之前首先要下载Web Part Templates for Visual Studio .NET:(http://www.microsoft.com/downloads/details.aspx?FamilyId=14D5D92F-C3A6-407C-AAD7-B8C41A4991BE&displaylang=en)。
Web部件与普通的asp.net自定义控件没有什么区别,如果你具有开发asp.net自定义控件的经验的话,开发Web部件就轻车熟路了。
下面是我自己开发的一个很简单的Web部件,如图右下角的SimpleWebPart(实现了简单的加法运算):
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
namespace MyWebParts
{
/// <summary>
/// Description for WebPart1.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:WebPart1 runat=server></{0}:SimpleWebPart>"),
XmlRoot(Namespace="MyWebParts")]
public class SimpleWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
protected TextBox txtValue1;
protected TextBox txtValue2;
protected TextBox txtResult;
protected Label lblOp;
protected Label lblEq;
protected Button btnCalc;
//private const string defaultText = "";
//private string text = defaultText;
// [Browsable(true),
// Category("Miscellaneous"),
// DefaultValue(defaultText),
// WebPartStorage(Storage.Personal),
// FriendlyName("Text"),
// Description("Text Property")]
// public string Text
// {
// get
// {
// return text;
// }
//
// set
// {
// text = value;
// }
// }
private bool calcEnabled = true;
[Browsable(true),
Description("是否启用计算功能!"),
WebPartStorage(Storage.Personal),
FriendlyName("允许计算"),
DefaultValue(true),
Category("自定义")
]
public bool CalcEnabled
{
get
{
return calcEnabled;
}
set
{
calcEnabled = value;
if( this.btnCalc != null )
{
this.btnCalc.Enabled = value;
}
}
}
/// <summary>
/// This method gets the custom tool parts for this Web Part by overriding the
/// GetToolParts method of the WebPart base class. You must implement
/// custom tool parts in a separate class that derives from
/// Microsoft.SharePoint.WebPartPages.ToolPart.
/// </summary>
///<returns>An array of references to ToolPart objects.</returns>
// public override ToolPart[] GetToolParts()
// {
// ToolPart[] toolparts = new ToolPart[2];
// WebPartToolPart wptp = new WebPartToolPart();
// CustomPropertyToolPart custom = new CustomPropertyToolPart();
// custom.Expand("自定义");
// toolparts[0] = custom;
// toolparts[1] = wptp;
// return toolparts;
// }
/// <summary>
/// Render this Web Part to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void RenderWebPart(HtmlTextWriter output)
{
//output.Write(SPEncode.HtmlEncode(Text));
EnsureChildControls();
output.RenderBeginTag("div");
//RenderChildren(output);
this.txtValue1.RenderControl(output);
output.AddStyleAttribute(HtmlTextWriterStyle.FontWeight,"bold");
output.RenderBeginTag("span");
this.lblOp.RenderControl(output);
output.RenderEndTag(); //span
this.txtValue2.RenderControl(output);
this.lblEq.RenderControl(output);
this.txtResult.RenderControl(output);
this.btnCalc.RenderControl(output);
output.RenderEndTag(); //div
}
protected override void CreateChildControls()
{
//初始化文本框
txtValue1 = new TextBox();
txtValue2 = new TextBox();
txtValue1.Text = "1";
txtValue2.Text = "2";
lblOp = new Label();
lblOp.Text = " + ";
lblEq = new Label();
lblEq.Text = " = ";
txtResult = new TextBox();
txtResult.ReadOnly = true;
btnCalc = new Button();
btnCalc.Text = "Calculate";
btnCalc.Click += new EventHandler(btnCalc_Click);
btnCalc.Enabled = calcEnabled;
Controls.Add(txtValue1);
Controls.Add(lblOp);
Controls.Add(txtValue2);
Controls.Add(lblEq);
Controls.Add(txtResult);
Controls.Add(btnCalc);
base.CreateChildControls ();
}
public void btnCalc_Click(object sender,System.EventArgs e)
{
if( txtValue1.Text.Trim().Length > 0 && txtValue2.Text.Trim().Length > 0 )
{
this.txtResult.Text = Convert.ToString(Convert.ToDouble(txtValue1.Text) + Convert.ToDouble(txtValue2.Text));
}
}
}
}
从上面的代码可以看出,一般来说自定义的WebPart 都继承自Microsoft.SharePoint.WebPartPages.WebPart,而不是继承自WebControl。
详细的可以参考:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/spptsdk/html/CreateABasicWP.asp