此示例演示如何添加可阻止从列表删除项目的简单事件处理程序。此任务包含两个过程:
-
在 Microsoft Visual Studio 中创建事件处理程序
-
添加事件处理程序作为 Windows SharePoint Services 中的功能
在 Visual Studio 中创建事件处理程序
-
通过单击“文件”,指向“新建”,然后单击“项目”以在 Visual Studio 中创建新项目。
-
在“新建项目”对话框中,选择“项目类型”框中的“Visual C#”,选择“模板”框中的“类库”,在“名称”框中键入“DeletingEventHandler”,然后单击“确定”。
-
在解决方案资源管理器中,选择“DeletingEventHandler”,然后在“项目”菜单上单击“添加引用”。
-
在“添加引用”对话框中,在“.NET”选项卡上选择 Microsoft.SharePoint,然后单击“确定”。
-
在代码编辑器中,导入 Microsoft.SharePoint 命名空间,如下所示。
using Microsoft.SharePoint;
-
将类的名称更改为 DeletingAction,并使其从 SPItemEventReceiver 类继承,如下所示。
public class DeletingAction : SPItemEventReceiver
-
在类中添加下面的代码以重写 ItemDeleting 方法。
C#public override void ItemDeleting(SPItemEventProperties properties) { properties.Cancel = true; properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported."; }
-
在解决方案资源管理器中,右键单击“DeletingEventHandler”节点,然后单击“属性”。
-
在“属性”对话框中,单击“签名”选项卡,选择“为程序集签名”,选择“选择强名称密钥文件”,然后单击“<新建…>”。
-
在“创建强名称密钥”对话框中,在“密钥文件名称”框中键入“DeletingEventHandler.snk”,(可选)指定密钥的密码,然后单击“确定”。
-
若要生成项目,请单击“生成”菜单上的“生成解决方案”,或按 Ctrl+Shift+B。
-
在 Visual Studio Projects 文件夹中查找
\DeletingEventHandler\bin\Debug
文件夹,并将 DeletingEventHandler.dll 文件拖动到Local_Drive:\WINDOWS\assembly
以将 DLL 放置在全局程序集缓存中。
将事件处理程序作为 Windows SharePoint Services 功能添加
-
在本地驱动器
:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/FEATURES
中创建一个名为“DeletingEventHandler”的文件夹。 -
在此文件夹中创建一个 Feature.xml 文件 文件(如下所示),此文件标识功能及其元素指令清单文件并将功能范围设置为 Web site。
Xml<Feature Scope="Web" Title="Deleting Event Handler" Id="GUID" xmlns="http://schemas.microsoft.com/sharepoint/"> <ElementManifests> <ElementManifest Location="Elements.xml"/> </ElementManifests> </Feature>
-
若要替换以前的 Id 属性中的 GUID 占位符,请通过运行位于本地驱动器
:\Program Files\Microsoft Visual Studio 8
中的 guidgen.exe 来生成 GUID。 -
在 DeletingEventHandler 文件夹中创建一个 Elements.xml 文件,此文件标识作为事件处理程序实现的程序集、类和方法。根据 ListTemplateId 属性所指定,此示例将事件处理程序应用于网站的所有通知列表。有关其他默认 Windows SharePoint Services 列表模板类型的 ID,请参阅 ListTemplate 元素的 Type 属性说明。
Xml<Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Receivers ListTemplateId="104"> <Receiver> <Name>DeletingEventHandler</Name> <Type>ItemDeleting</Type> <SequenceNumber>10000</SequenceNumber> <Assembly>DeletingEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a26b5449ac4a4cf3</Assembly> <Class>DeletingEventHandler.DeletingAction</Class> <Data></Data> <Filter></Filter> </Receiver> </Receivers> </Elements>
-
若要获取程序集的公钥标记,请在 Windows 资源管理器中查找位于本地驱动器
:\WINDOWS\assembly
中的 DeletingEventHandler.dll 文件,右键单击此文件,单击“属性”,然后在“属性”对话框的“常规”选项卡上,选择该标记并将其复制。 -
在命令提示符下,导航到本地驱动器上的
\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN
,键入下面的每个命令以在部署中安装功能,并在指定子网站上激活功能,然后重置 Microsoft Internet Information Services (IIS),从而使所做更改生效:stsadm -o installfeature -filename DeletingEventHandler\Feature.xml stsadm -o activatefeature -filename DeletingEventHandler\Feature.xml -url http://Server/Site/Subsite iisreset
-
尝试删除指定网站上的通知列表中的项目,以查看事件处理程序功能的效果。
参照MSDN-如何:创建事件处理程序功能:http://msdn.microsoft.com/zh-cn/ms453149.aspx