SharePoint – 功能\事件接收器
SharePoint – 功能\事件接收器
在本章中,我们将学习添加代码句柄。代码句柄是在激活或停用功能时引发的事件。换句话说,我们将检查Feature Receivers。
我们在上一章中创建的 Visual Studio 项目有一个功能,当它被激活时,它提供了我们的联系人列表、我们的站点页面和指向站点页面的链接。
但是,当停用该功能时,SharePoint 仅删除链接,站点页面和联系人列表仍保留。
如果需要,我们可以在功能停用时编写代码以删除列表和页面。在本章中,我们将学习如何在禁用功能时删除内容和元素。
为了处理功能的事件,我们需要一个功能接收器。
步骤 1 – 要获取功能接收器,请右键单击解决方案资源管理器中的功能,然后选择添加事件接收器。
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
namespace FeaturesAndElements.Features.Sample {
/// <summary>
/// This class handles events raised during feature activation, deactivation,
installation, uninstallation, and upgrade.
/// </summary>
/// <remarks>
/// The GUID attached to this class may be used during packaging and should not be modified.
/// </remarks>
[Guid("e873932c-d514-46f9-9d17-320bd3fbcb86")]
public class SampleEventReceiver : SPFeatureReceiver {
// Uncomment the method below to handle the event raised after a feature has been activated.
//public override void FeatureActivated(SPFeatureReceiverProperties properties)//{
//
}
// Uncomment the method below to handle the event raised before a feature is deactivated.
//public override void FeatureDeactivating(SPFeatureReceiverProperties properties)// {
//
}
// Uncomment the method below to handle the event raised after a feature has been installed.
//public override void FeatureInstalled(SPFeatureReceiverProperties properties)// {
//
}
// Uncomment the method below to handle the event raised before a feature is uninstalled.
//public override void FeatureUninstalling(SPFeatureReceiverProperties properties)// {
//
}
// Uncomment the method below to handle the event raised when a feature is upgrading.
//public override void FeatureUpgrading(SPFeatureReceiverProperties
properties, string upgradeActionName,
System.Collections.Generic.IDictionary<string, string> parameters) // {
//
}
}
}
你可以看到我们得到的是一个继承自SPFeatureReceiver的类。
在 SharePoint 中,您可以处理不同类型的事件的不同类。例如,列表上的事件、列表项上的事件、站点上的事件。您可以创建一个派生自特定事件接收器的类,然后您可以覆盖该类中的方法来处理事件。
功能的事件在它被使用时 –
- 活性
- 停用
- 已安装
- 已卸载
- 升级
接下来,您需要将该类附加为特定项目的事件处理程序。例如,如果有处理列表事件的事件处理程序,则需要将该类附加到列表。
因此,我们将处理两个功能 –
-
当该功能被激活并且
-
当它被停用时。
第 2 步– 我们将实现FeatureActivated和 FeatureDeactivated 方法,如下所示 –
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
namespace FeaturesAndElements.Features.Sample {
/// <summary>
/// This class handles events raised during feature activation, deactivation,
installation, uninstallation, and upgrade.
/// </summary>
/// <remarks>
/// The GUID attached to this class may be used during packaging and should
not be modified.
/// </remarks>
[Guid("e873932c-d514-46f9-9d17-320bd3fbcb86")]
public class SampleEventReceiver : SPFeatureReceiver {
private const string listName = "Announcements";
public override void FeatureActivated(SPFeatureReceiverProperties properties) {
var web = properties.Feature.Parent as SPWeb;
if (web == null) return;
var list = web.Lists.TryGetList(listName);
if (list != null) return;
var listId = web.Lists.Add(listName, string.Empty,
SPListTemplateType.Announcements);
list = web.Lists[listId];
list.OnQuickLaunch = true;
list.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
var web = properties.Feature.Parent as SPWeb;
if (web == null) return;
var list = web.Lists.TryGetList(listName);
if (list == null) return;
if (list.ItemCount == 0) {
list.Delete();
}
}
}
}
注意–
-
当该功能被激活时,我们将创建一个公告列表。
-
当该功能被停用时,我们将检查公告列表是否为空,如果是,我们将删除它。
步骤 3 – 现在右键单击项目并选择部署。您将看到以下部署冲突警告。

Visual Studio 告诉我们我们正在尝试创建一个名为联系人的列表,但站点中已经有一个名为联系人的列表。它询问我们是否要覆盖现有列表,在这种情况下单击Resolve。
第 4 步– 返回 SharePoint,然后刷新您的站点并转到站点操作 → 站点设置 → 管理站点功能 → 示例功能。

您可以看到左侧窗格中没有公告列表。
第 5 步– 让我们激活示例功能,您将看到公告列表,但现在它是空的。

注意– 如果您停用您的示例功能,那么您会注意到公告列表消失了。
第 6 步– 让我们重新激活该功能。转到公告,然后添加新公告。我们将调用此测试,然后单击保存。

您将在公告下看到测试文件。

现在,当您停用公告时,您将看到公告列表保持不变,因为它不是空的。