SharePoint - Feature\Event Receiver

在本章中,我们将学习添加代码句柄.代码句柄是激活或取消激活功能时引发的事件.换句话说,我们将检查功能接收器.

我们在上一章中创建的Visual Studio项目有一个功能,当它被激活时,它配置了我们的联系人列表,我们的SitePage以及SitePage的链接.

但是,当功能停用时,SharePoint仅删除链接,SitePage和联系人列表仍然保留./p>

如果我们愿意,我们可以在停用功能时编写代码以删除列表和页面.在本章中,我们将学习如何在停用功能时删除内容和元素.

要处理功能的事件,我们需要功能接收器.

第1步 : 要获取Feature接收器,请右键单击Solution Explorer中的Feature,然后选择添加事件接收器.

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中,您可以处理不同类型的事件的不同类.例如,列表上的事件,列表项上的事件,站点上的事件.您可以创建一个派生自特定事件接收器的类,然后您可以覆盖该类内部的方法来处理事件.

特征的事件在被使用时使用 :

  • 已激活

  • 已停用

  • 已安装

  • 已卸载

  • 升级

接下来,您需要附加class作为特定项的事件处理程序.例如,如果有一个处理列表事件的事件处理程序,则需要将该类附加到列表中.

因此,我们将处理两个功能 :

  • 当此功能被激活且

  • 当它被取消激活时.

第2步 : 我们将使用System实现 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步 : 现在右键单击Project并选择deploy.您将看到以下部署冲突警告.

部署冲突

Visual Studio告诉我们,我们正在尝试创建一个名为contacts的列表,但该站点中已有一个名为Contacts的列表.它询问我们是否要覆盖现有列表,在这种情况下,请单击解决.

步骤4 : 返回SharePoint,然后刷新您的站点并转到 Site Actions → 网站设置 → 管理网站功能 → 示例功能.

示例功能

你可以看到左窗格中没有公告列表.

第5步 : 让我们激活示例功能,您将看到公告列表,但它现在是空的.

公告列表

注意 : 如果您停用样品功能,则会注意到通知列表消失.

步骤6 : 让我们重新激活该功能.转到公告,然后添加新公告.我们将调用此测试,然后单击"保存".

新公告

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

公告下的测试文件

现在当你停用公告时,你会看到公告列表因为它不是空的而停留.

停用公告