SharePoint - 功能/事件接收器



在本章中,我們將學習新增程式碼控制代碼。程式碼控制代碼是在啟用或停用功能時觸發的事件。換句話說,我們將檢查功能接收器

我們在上一章建立的 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 - 現在右鍵單擊專案並選擇部署。您將看到以下部署衝突警告。

Deployment Conflicts

Visual Studio 告訴我們我們正在嘗試建立一個名為聯絡人的列表,但站點中已存在一個名為聯絡人的列表。它詢問我們是否要覆蓋現有列表,在這種情況下,請單擊解決

步驟 4 - 返回到 SharePoint,然後重新整理您的網站並轉到網站操作→網站設定→管理網站功能→示例功能

Sample Features

您可以在左側窗格中看到沒有公告列表。

步驟 5 - 讓我們啟用示例功能,您將看到公告列表,但現在它是空的。

Announcements List

注意 - 如果您停用示例功能,則會注意到公告列表消失。

步驟 6 - 讓我們重新啟用該功能。轉到公告,然後新增新公告。我們將稱之為測試,然後單擊儲存。

New Announcement

您將在公告下看到測試檔案。

Test File Under Announcements

現在,當您停用公告時,您會看到公告列表仍然存在,因為它不為空。

Deactivate Announcements
廣告

© . All rights reserved.