共用方式為


SPEventReceiverDefinition class

定義一般內容的清單項目、 清單、 網站及工作流程事件接收器的抽象基底類別。

Inheritance hierarchy

System.Object
  Microsoft.SharePoint.Administration.SPAutoSerializingObject
    Microsoft.SharePoint.SPEventReceiverDefinition

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'宣告
Public NotInheritable Class SPEventReceiverDefinition _
    Inherits SPAutoSerializingObject
'用途
Dim instance As SPEventReceiverDefinition
public sealed class SPEventReceiverDefinition : SPAutoSerializingObject

備註

管理事件接收者回應每當特定觸發動作的SharePoint Foundation事件的程式碼就會發生。觸發SharePoint Foundation物件包含清單項目、 清單或文件庫。觸發的動作包括活動,例如新增、 移動、 或簽出。應該會收到事件的物件是事件主應用程式是網站集合、 網站、 清單、 工作流程或功能等物件。SharePoint Foundation事件被分成兩個類別: Before和After事件。中SharePoint Foundation, Before事件名為"...ing"並命名為After事件"...ed 」。Before事件引發之前SharePoint Foundation將資料寫入備份內容資料庫,就會發生在使用者動作的回應。它們允許由一個實作來執行安全性檢查和自訂驗證,因此它們在支援取消使用者動作的要求處理生命週期的早期就會發生。所有的Before事件接收器是同步的而且它們封鎖的工作執行緒的執行流程的事件處理常式到完成為止。After事件都有執行後SharePoint Foundation藉由將資料寫回到內容資料庫認可的使用者動作的事件處理常式。After事件並不支援取消使用者動作。當引發After事件可能會發生的事件處理的兩種類型: 同步和非同步。同步處理提供傳送回瀏覽器的 Web 回應之前,在相同的執行緒執行事件的能力。非同步事件接收者處理方式是不同的執行緒,以便處理不會封鎖的程式碼的執行流程。非同步處理的個別工作執行緒會存放在SPEventReceiverDefinition類別以外的類別的方法來啟動。

使用SPContentTypeSPFileSPListSPSiteSPWebSPWorkflow類別的EventReceivers屬性來取得事件接收器註冊為指定的Microsoft SharePoint Foundation物件的集合。一旦您有事件接收器 (的集合型別SPEventReceiverDefinitionCollection) 的參考,您可以使用它來加入新的事件接收器定義或擷取現有的平台。使用Add()方法來建立新的事件SPEventReceiverDefinition物件。然後設定不同的屬性定義,組成,並呼叫Update()方法來儲存事件接收器定義。

若要從集合傳回單一事件接收器定義中使用索引子。例如,如果集合指派給一個名為collEventReceiverDefinitions變數中,使用在 C# 的collEventReceiverDefinitions[index]或在 Visual Basic.NET 中,其中index是集合中定義的索引編號,或是定義的 GUID 的collEventReceiverDefinitions(index) 。

Examples

下列範例會使用登錄指定的網站上的 [連絡人] 清單同步ItemAdding事件接收者SPEventReceiverDefinition類別的成員。

string listName = "Contacts";
string siteURL = "https://Server/Site";
string receiverName = "Contacts Event Receiver";
int sequenceNumber = 2001;
string assemblyFullName = "Assembly_Name, Version=1.0.1777.23493, Culture=neutral, PublicKeyToken=94de0004b6e3fcc5";
string assemblyClassName = "Assembly_Name.Class_Name";
string receiverData = "Data";

SPList list = new SPSite(siteURL).OpenWeb().Lists[listName];
SPEventReceiverDefinitionCollection eventReceivers = list.EventReceivers;

using(SPSite site = new SPSite(siteURL))
{ 
   using (SPWeb web = site.OpenWeb()) 
   {
      SPList list = new SPSite(siteURL).OpenWeb().Lists[listName]; 
      SPEventReceiverDefinitionCollection eventReceivers =
         list.EventReceivers;

      SPEventReceiverDefinition eventReceiver = eventReceivers.Add();
      eventReceiver.Name = receiverName;
      eventReceiver.Synchronization =
         SPEventReceiverSynchronization.Synchronous; 
      eventReceiver.Type = SPEventReceiverType.ItemAdded;
      eventReceiver.SequenceNumber = sequenceNumber; 
      eventReceiver.Assembly = assemblyFullName ;
      eventReceiver.Class = assemblyClassName ;
      eventReceiver.Data = receiverData ;

      eventReceiver.Update();
   }
}
Dim listName As String = "Contacts"
Dim siteURL As String = "https://Server/Site"
Dim receiverName As String = "Contacts Event Receiver"
Dim sequenceNumber As Integer = 2001
Dim assemblyFullName As String = "Assembly_Name, Version=1.0.1777.23493, Culture=neutral, PublicKeyToken=94de0004b6e3fcc5"
Dim assemblyClassName As String = "Assembly_Name.Class_Name"
Dim receiverData As String = "Data"

Dim list As SPList = New SPSite(siteURL).OpenWeb().Lists(listName)
Dim eventReceivers As SPEventReceiverDefinitionCollection = list.EventReceivers

Using site As New SPSite(siteURL)
   Using web As SPWeb = site.OpenWeb()
     Dim list As SPList = New SPSite(siteURL).OpenWeb().Lists(listName)
     Dim eventReceivers As SPEventReceiverDefinitionCollection = list.EventReceivers

     Dim eventReceiver As SPEventReceiverDefinition = eventReceivers.Add()
     eventReceiver.Name = receiverName
     eventReceiver.Synchronization = SPEventReceiverSynchronization.Synchronous
     eventReceiver.Type = SPEventReceiverType.ItemAdded
     eventReceiver.SequenceNumber = sequenceNumber
     eventReceiver.Assembly = assemblyFullName
     eventReceiver.Class = assemblyClassName
     eventReceiver.Data = receiverData

     eventReceiver.Update()
   End Using
End Using

Thread safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

請參閱

參照

SPEventReceiverDefinition members

Microsoft.SharePoint namespace

SPEventType