开发 SharePoint WebParts






4.91/5 (15投票s)
SharePoint 2007 Web Part 开发的四个重要方面。

引言
开发 SharePoint Web 部件是自定义 SharePoint 平台的最佳方式之一。Web 部件将 ASP.NET 的强大功能与 Web 部件页面的灵活性相结合。本文旨在帮助您快速上手 Web 部件开发。
我们将简要介绍 Web 部件开发中的四个主题
- 设置 Visual Studio SharePoint 开发环境。
- 创建和渲染 Web 部件中的 ASP.NET 控件
- 使用
Microsoft.SharePoint
命名空间访问 SharePoint 数据 - 使用 设置 API,以便管理员用户可以在生产环境中配置和管理 Web 部件。
背景
需要了解 SharePoint、ASP.NET 和 Visual Studio。
创建 Visual Studio SharePoint 开发环境
本节将介绍设置开发环境的步骤。如果您已经拥有可用的开发环境,则可以安全地 跳过本节。
为了遵循这些步骤,您需要 Visual Studio 2005(.NET 2.0)或更高版本。您还需要网络访问一台安装了 SharePoint 2007(WSS 或 MOSS)的 Windows 2003/8 服务器 - SharePoint 不支持 XP 或 Vista。我通常使用一台安装了 .NET 3.5 和 Windows SharePoint Services 3.0 的 Windows 2003 虚拟机。也可以在开发服务器上安装 Visual Studio 2005/8。
我们假设您在 SharePoint 服务器和目标 SharePoint 网站应用程序中都拥有管理员权限。
设置 Visual Studio Web 部件开发环境的步骤如下
- 在 Visual Studio 中,创建一个新的类库
- 添加对
System.Web
的引用 - 在此步骤中,我们将创建一个简单的 Web 部件,该部件在屏幕上显示“Hello World!”。Web 部件是一个继承自
System.Web.UI.WebControls.WebParts
中WebPart
类的类。为了控制 Web 部件的渲染,需要重写Render
方法。一个HtmlTextWriter
实例会被传递到 Render 方法中,然后可以使用它向页面输出写入标记。下面的代码是一个示例using System; using System.Collections.Generic; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; namespace ExampleWebPartLibrary { public class MyWebPart : WebPart { protected override void Render(HtmlTextWriter writer) { writer.Write("Hello World!"); } } }
- 使用 Visual Studio 或其他合适的工具,为您的类库指定强名称密钥并获取程序的完整名称。您可以在 Visual Studio 命令提示符中使用“gacutil /l”获取完整名称,或者使用 Lutz Roeder 的 .NET Reflector。
- 在类库属性中,将构建输出路径设置为 SharePoint 开发网站应用程序的 bin 目录。我通常在开发服务器上共享目录为“\\SharePointDev\wss_bin”,并使用此目录作为构建输出目录。在 Windows SharePoint Services 3.0 中,bin 目录通常的形式是“C:\Inetpub\wwwroot\wss\VirtualDirectories\[Port Num]\bin”,我将此目录共享为“wss_bin”。
- 在 AssemblyInfo.cs 文件中添加代码语句
using System.Security;
和[assembly: AllowPartiallyTrustedCallers]
。 - 确保您的类库能够成功构建,并且 Visual Studio 将 DLL 放置在服务器上的预期位置。
- 在目标服务器的 web.config 文件中,您需要将程序集添加为安全控件。您可以复制一个现有的安全控件条目,并用您的程序集的详细信息进行填充。您将需要程序的完整名称来完成此步骤。
<SafeControl Assembly="ExampleWebPartLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=db485f68ad2dc0c5" Namespace="ExampleWebPartLibrary" TypeName="*" Safe="True" AllowRemoteDesigner="True" />
- 现在您可以将 Web 部件库添加到您的 SharePoint 网站。要执行此操作,请以管理员身份登录并转到设置页面。在“库”部分下单击“Web 部件库”。单击“新建”,您应该会在列表中看到您的 Web 部件。选择您的 Web 部件并单击“填充库”。
- 您可以通过 Web 部件库中的编辑按钮为您的 Web 部件设置一个友好名称并控制其权限。我选择将 Web 部件命名为“My Web Part”。
- 最后,您可以将 Web 部件添加到 Web 部件页面,您应该会看到 Web 部件硬编码的“Hello World!”消息。
如果您的构建目标设置为 SharePoint bin 目录(如上述列表中的第 5 步),那么在测试更改时无需重新部署。每次重新构建类库时,您可以通过刷新 Web 部件页面来测试 Web 部件的最新版本。
每当向库中添加新的 Web 部件时,您也不需要重复上述步骤。每次向库中添加新的 Web 部件时,您只需用您的新 Web 部件“填充库”即可。
可以使用远程调试来调试 Web 部件。我们在此不做详细介绍;MSDN 库中有一篇关于此主题的文章。
在 Web 部件中使用 ASP.NET 控件
在 Web 部件开发中,您没有 Visual Studio ASP.NET 代码生成的便利。您必须在 Web 部件生命周期的正确阶段声明、初始化、配置和渲染控件。在此示例中,我们将创建一个按钮和一个标签,并使用按钮点击事件来更改标签文本。
控件应声明为成员变量,然后在重写 CreateChildControls
方法时进行初始化和配置。我通常会创建一个单独的方法来设置每个控件,然后从 CreateChildControls
调用该方法。将创建的控件添加到 Web 部件控件列表中非常重要。如果控件未添加到此列表中,则在回发时不会引发其事件。
private Button btnDemo; private void createBtnDemo() { btnDemo = new Button(); btnDemo.Text = "Click me"; btnDemo.Click += new EventHandler(btnDemo_Click); // This line of code is required to trigger the click event Controls.Add(btnDemo); } protected override void CreateChildControls() { base.CreateChildControls(); createBtnDemo(); createLblHelloWorld(); }
控件在 Render
方法重写中进行渲染。此方法可用于布局 Web 部件及其控件。HtmlTextWriter
拥有许多有用的方法来提供帮助,包括 WriteBreak()
,它会写入一个“<br />”。
protected override void Render(HtmlTextWriter writer) { writer.WriteBreak(); lblHelloWorld.RenderControl(writer); writer.WriteBreak(); writer.WriteBreak(); btnDemo.RenderControl(writer); }注意: Web 部件开发缺乏 Visual Studio 支持的一个已知解决方法是在 Web 部件中嵌入 UserControls。您可以在 此 Code Project 文章中阅读相关内容。
在 Web 部件中访问 SharePoint 数据
为了从 Web 部件中访问 SharePoint 数据,您需要执行两个步骤。第一步是引用 Microsoft.SharePoint.dll。您可以通过浏览到“[Program Files]\Common Files\Microsoft Shared\web server extensions\12\ISAPI”目录从 SharePoint 服务器获取此 DLL 的副本。
第二步是处理 Web 部件的代码访问安全性。最简单的方法是将目标 SharePoint 网站的信任级别设置为“完全”。这种信任级别通常在内部网环境中是可以接受的。如果您要求 SharePoint 网站在最低或中等信任级别环境中运行,那么您将需要手动配置您的代码访问安全性。
要选择更简单的方法,请将网站信任级别设置为“完全”,在您网站的 web.config 文件中找到以下内容
<trust level="WSS_Minimal" originUrl="" />
现在将其更改为
<trust level="Full" originUrl="" />
现在您可以编写一些代码来访问 SharePoint 数据。这仅仅是学习和理解 SharePoint API 的问题,它本身就值得撰写多篇文章。这里我展示了一个简单的 Web 部件,它显示当前登录用户的名字
using System.Web.UI.WebControls.WebParts; using Microsoft.SharePoint; namespace ExampleWebPartLibrary { public class HelloUser : WebPart { protected override void Render(System.Web.UI.HtmlTextWriter writer) { // Get a contextual reference to the current SPWeb SPWeb currentWeb = SPContext.Current.Web; // Write a message to the current user writer.Write("Hello " + currentWeb.CurrentUser.LoginName); } } }
SPContext.Current.Web
是访问 SharePoint 数据的起点。例如,您可以使用它来访问列表和文档库中的数据。
使用 SharePoint 设置来管理您的 Web 部件

Web 部件为了实用性,不可避免地需要设置。有两种方法可以在运行时为 Web 部件提供设置。在定制环境中,可以将设置放在 web.config 中,并以与标准 ASP.NET 应用程序相同的方式访问它们。但是,如果您要求用户能够轻松地自行配置和管理 Web 部件,那么您就需要使用 SharePoint API 为 Web 部件提供设置。如果您是独立软件供应商,正在发布您 Web 部件的打包版本,那么几乎肯定会是这种情况。
幸运的是,为 Web 部件启用设置功能相当容易。以下步骤假设您是从现有 Web 部件开始的,但如果您是从零开始,则可以跳到文章末尾的列表。
- 添加所需的 using 语句
using System; using System.ComponentModel; using System.Runtime.InteropServices; using System.Xml.Serialization; using Microsoft.SharePoint; using Microsoft.SharePoint.WebPartPages;
- 将您的 Web 部件更改为继承自
Microsoft.SharePoint.WebPartPages.WebPart
- 为您的 Web 部件创建一个新的 guid。像这样向您的 Web 部件添加
Guid
和XmlRoot
属性[Guid("692b5ada-2f4c-45b0-b9a5-e718db958ae6")] [XmlRoot(Namespace = "ExampleWebPartLibrarySettingsDemo")] public class SettingsDemo : Microsoft.SharePoint.WebPartPages.WebPart
- 为用作设置的属性添加不同的属性。根据您的需要调整常量字符串属性参数。
private string mySetting; // SharePoint setting with required attributes [Browsable(true), Category("Example Web Parts"), DefaultValue("Default"), WebPartStorage(Storage.Shared), FriendlyName("MySetting"), Description("An example setting")] public string MySetting { get { return mySetting; } set { mySetting = value; } }
- 在代码中使用设置(在本例中,是 render 方法)。
完整的代码列表如下
// using statements required for settings using System; using System.ComponentModel; using System.Runtime.InteropServices; using System.Xml.Serialization; using Microsoft.SharePoint; using Microsoft.SharePoint.WebPartPages; namespace ExampleWebPartLibrary { //Guid and XmlRoot attributes for a settings enabled web part [Guid("692b5ada-2f4c-45b0-b9a5-e718db958ae6")] [XmlRoot(Namespace = "ExampleWebPartLibrarySettingsDemo")] // NB inherit from SharePoint WebPart rather than ASP.NET Web Part public class SettingsDemo : Microsoft.SharePoint.WebPartPages.WebPart { private string mySetting; // SharePoint setting with required attributes [Browsable(true), Category("Example Web Parts"), DefaultValue("Default"), WebPartStorage(Storage.Shared), FriendlyName("MySetting"), Description("An example setting")] public string MySetting { get { return mySetting; } set { mySetting = value; } } // use the setting in code protected override void Render(System.Web.UI.HtmlTextWriter writer) { writer.Write("Value of MySetting: "); writer.Write(mySetting); } } }
深入阅读
- MSDN 文章 ASP.NET Web 部件控件
- Web 部件开发人员入门指南
- Code Project: Web 部件生命周期
- Code Project: 用于在 SharePoint 2007 中嵌入 Flash 的 Web 部件