自己动手做一个简单的WebPart
首先确认在机器上已经安装了如下东西
Microsoft® Windows® SharePoint™ Services
Microsoft Office SharePoint Portal Server 2003(可选)
Microsoft Visual Studio® .NET
Web Part infrastructure
当你机器上面安装SharePoint Products and Technologies Templates: Web Part Templates for Visual Studio .NET之后,你新建Visual C#或者是Visual Basic项目的时候,如图:
当你选择该模板并创建工程的时候,Visual Studio会自动创建一个包含以下3个文件的解决方案,如图:
Manifest.xml和WebPart1.dwp是XML格式的配置文件,记录了关于WebPart的一些信息,WebPart1.cs自然就是代码文件。
打开WebPart1.cs文件之后,会看见如下代码:
写一个简单的WebPart,仅仅来显示一些超连接信息,代码如下:
Microsoft® Windows® SharePoint™ Services
Microsoft Office SharePoint Portal Server 2003(可选)
Microsoft Visual Studio® .NET
Web Part infrastructure
当你机器上面安装SharePoint Products and Technologies Templates: Web Part Templates for Visual Studio .NET之后,你新建Visual C#或者是Visual Basic项目的时候,如图:
当你选择该模板并创建工程的时候,Visual Studio会自动创建一个包含以下3个文件的解决方案,如图:
Manifest.xml和WebPart1.dwp是XML格式的配置文件,记录了关于WebPart的一些信息,WebPart1.cs自然就是代码文件。
打开WebPart1.cs文件之后,会看见如下代码:
namespace WebPartLibrary1
{
/// <summary>
/// Description for WebPart1.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:WebPart1 runat=server></{0}:WebPart1>"),
XmlRoot(Namespace="WebPartLibrary1")]
public class WebPart1 : Microsoft.SharePoint.WebPartPages.WebPart
{
private const string defaultText = "";
private string text = defaultText;
[Browsable(true), //是否在Panel中可见
Category("Miscellaneous"),// 所属类别
DefaultValue(defaultText),//默认值
WebPartStorage(Storage.Personal),//枚举Storage.Shared,Storage.Personal,Storage.None
FriendlyName("Text"),///显示在Panel中的名称
Description("WebPart1")] //描述
public string Text
{
get
{
return text;
}
set
{
text = 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();
// toolparts[0] = wptp;
// toolparts[1] = custom;
// 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));
}
}
}
{
/// <summary>
/// Description for WebPart1.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:WebPart1 runat=server></{0}:WebPart1>"),
XmlRoot(Namespace="WebPartLibrary1")]
public class WebPart1 : Microsoft.SharePoint.WebPartPages.WebPart
{
private const string defaultText = "";
private string text = defaultText;
[Browsable(true), //是否在Panel中可见
Category("Miscellaneous"),// 所属类别
DefaultValue(defaultText),//默认值
WebPartStorage(Storage.Personal),//枚举Storage.Shared,Storage.Personal,Storage.None
FriendlyName("Text"),///显示在Panel中的名称
Description("WebPart1")] //描述
public string Text
{
get
{
return text;
}
set
{
text = 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();
// toolparts[0] = wptp;
// toolparts[1] = custom;
// 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));
}
}
}
写一个简单的WebPart,仅仅来显示一些超连接信息,代码如下:
protected override void RenderWebPart(HtmlTextWriter output)
{
output.RenderBeginTag("div");
System.Collections.Hashtable hashTable = new System.Collections.Hashtable();
hashTable.Add("My SharePoint Blog","more.asp?name=blurlzy");
hashTable.Add("Another Blog In CnBlogs","http://www.cnblogs.com/blurlzy");
foreach(System.Collections.DictionaryEntry item in hashTable)
{
System.Web.UI.WebControls.HyperLink hyperLink = new HyperLink();
hyperLink.Text = item.Key.ToString();
hyperLink.NavigateUrl = item.Value.ToString();
output.RenderBeginTag("p");
hyperLink.RenderControl(output);
output.RenderEndTag();
}
output.RenderEndTag();
}
{
output.RenderBeginTag("div");
System.Collections.Hashtable hashTable = new System.Collections.Hashtable();
hashTable.Add("My SharePoint Blog","more.asp?name=blurlzy");
hashTable.Add("Another Blog In CnBlogs","http://www.cnblogs.com/blurlzy");
foreach(System.Collections.DictionaryEntry item in hashTable)
{
System.Web.UI.WebControls.HyperLink hyperLink = new HyperLink();
hyperLink.Text = item.Key.ToString();
hyperLink.NavigateUrl = item.Value.ToString();
output.RenderBeginTag("p");
hyperLink.RenderControl(output);
output.RenderEndTag();
}
output.RenderEndTag();
}
部署:
首先,在虚拟站点根目录的Web.config的<SafeControls>中增加你自己的WebPart,比如对于我这个WebPart:
<!--Customize WebPart -->
<SafeControl Assembly="WebPartLibrary1" Namespace="WebPartLibrary1" TypeName="*" Safe="True" />
然后将VS.NET编译出的.dll文件拷贝到SPS虚拟站点根目录的“bin”目录中,并在WebPart页面上导入WebPart。
选择.dwp文件
基本上一个简单的WebPart便会出现在页面上
这个WebPart的功能是非常的简单,甚至没什么实用价值,只是拿来作个例子,大致的描述一下做一个WebPart所需要的步骤。以后会陆续写一写关于开发WebPart的东西,也希望可以同大家多交流。