我已經讀過這個問題how to attach an event receiver to a custom list in sharepoint?但我有疑問。如何將事件處理程序附加到SharePoint中的特定列表?
當我使用EventReceivers.Add
方法附上我的事件接收器,並在事件接收器功能必須被激活,或者是它足以安裝了它?
而且,如果必須安裝,我應該在事件功能的elements.xml中使用哪個ListTemplateId?
在此先感謝
我已經讀過這個問題how to attach an event receiver to a custom list in sharepoint?但我有疑問。如何將事件處理程序附加到SharePoint中的特定列表?
當我使用EventReceivers.Add
方法附上我的事件接收器,並在事件接收器功能必須被激活,或者是它足以安裝了它?
而且,如果必須安裝,我應該在事件功能的elements.xml中使用哪個ListTemplateId?
在此先感謝
事件接收器是一個定義。該定義映射到程序集和類實例。在使用列表模板時,聲明性地使用一個用於事件接收器關聯的特性(指定listtemplateid)。當您根據不唯一的列表類型附加到特定列表時,通常使用對象模型。例如,通過使用TemplateTypeID ='104',將與具有該模板類型的所有列表相關聯。
使用對象模型可以識別特定的實例,以便您可以簡單地創建關聯。所以,如果你想部署一個事件接收器作爲一個功能,你將有一個功能加載程序集和一個功能接收器代碼,通過對象模型爲你創建關聯。
希望這是有道理的。
您只需要爲您的事件接收器部署解決方案。這使得代碼可以附加到列表中。
要將事件接收器連接到特定的列表在控制檯應用程序使用下面的代碼:
using (SPSite site = new SPSite(url))
{
using (SPWeb siteWeb = site.OpenWeb())
{
SPList list = siteWeb.Lists["TheList"];
SPEventReceiverDefinition defItemAdding = list.EventReceivers.Add();
defItemAdding.Assembly = "MyEventHandlerProject, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=placeTokenHere";
defItemAdding.Class = "MyEventHandlerProject.ClassName";
defItemAdding.Name = "ItemAdding Event";
defItemAdding.Type = SPEventReceiverType.ItemAdding;
defItemAdding.SequenceNumber = 1000;
defItemAdding.Synchronization = SPEventReceiverSynchronization.Synchronous;
defItemAdding.Update();
}}
大功告成!
您可以創建一個將事件處理程序附加到Sharepoint列表的控制檯應用程序。檢查下面的鏈接以獲取示例控制檯應用程序的代碼。
http://ceprogrammingnotebook.blogspot.sg/2013/10/attaching-event-handler-to-sharepoint.html
現在很清楚,謝謝! – Daniel 2011-03-17 18:10:40