nescafeboy1 发表于 2015-9-25 00:43:37

SharePoint事件处理器

  以下内容是对SPEventReceiver的一点总结
  一、概述
  五种事件处理器基类:
  1、SPItemEventReceiver
  2、SPListEventReceiver
  3、SPEmailEventReceiver
  4、SPWebEventReceiver
  5、SPWorkflowEventReceiver
  
  SPItemEventReceiver包含如下方法:






  Name


  Description




  ContextEvent


  This member is reserved for internal use and is not intended to be used directly from your code.




  DisableEventFiring


  Obsolete. Prevents events from being raised. (Inherited from SPEventReceiverBase.)




  EnableEventFiring


  Obsolete. Enables events to be raised. (Inherited from SPEventReceiverBase.)




  Equals


  (Inherited from Object.)




  Finalize


  (Inherited from Object.)




  GetHashCode


  (Inherited from Object.)




  GetType


  (Inherited from Object.)




  ItemAdded


  Handles the asynchronous event that occurs after an item is added.




  ItemAdding


  Handles the synchronous event that occurs before an item is added.




  ItemAttachmentAdded


  Handles the asynchronous event that occurs after an attachment is added to an item.




  ItemAttachmentAdding


  Handles the synchronous event that occurs before an attachment is added to an item.




  ItemAttachmentDeleted


  Handles the asynchronous event that occurs after an attachment is removed from an item.




  ItemAttachmentDeleting


  Handles the synchronous event that occurs before an attachment is removed from an item.




  ItemCheckedIn


  Handles the asynchronous event that occurs after an item is checked in.




  ItemCheckedOut


  Handles the asynchronous event that occurs after an item is checked out.




  ItemCheckingIn


  Handles the synchronous event that occurs before an item is checked in.




  ItemCheckingOut


  Handles the synchronous event that occurs before an item is checked out.




  ItemDeleted


  Handles the asynchronous event that occurs after an item is deleted.




  ItemDeleting


  Handles the synchronous event that occurs before an item is deleted.




  ItemFileConverted


  Handles the asynchronous event that occurs after a file in a document library is converted from one type to another.




  ItemFileMoved


  Handles the asynchronous event that occurs after a file is moved.




  ItemFileMoving


  Handles the synchronous event that occurs before a file is moved.




  ItemUncheckedOut


  Handles the asynchronous event that occurs after an item is unchecked out.




  ItemUncheckingOut


  Handles the synchronous event that occurs before an item checkout is discarded.




  ItemUpdated


  Handles the asynchronous event that occurs after an item is changed.




  ItemUpdating


  Handles the synchronous event that occurs before an item is changed.




  ItemVersionDeleted


  Occurs after an item or file version is deleted.




  ItemVersionDeleting


  Occurs when an item or file version is being deleted.




  MemberwiseClone


  (Inherited from Object.)




  ToString


  (Inherited from Object.)






源文档 <http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spitemeventreceiver_methods.aspx>  每个方法都有一个SPItemEventProperties参数,包含很多关于提交记录的相关信息。
  具体信息请参考MSDN文档 http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spitemeventproperties_members.aspx
  
  二、结构分析(以SPItemEventReceiver为例):


View Code


namespace CustomEventReceiver.EventReceiver1
{
/// <summary>
/// 列表项事件
/// </summary>
public class EventReceiver1 : SPItemEventReceiver
{
/// <summary>
/// 正在添加项.
/// </summary>
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
}
}
}
  生成的事件处理代码,继承自SPItemEventReceiver,重写ItemAdding(添加Item前的事件)代码。


View Code


<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Receivers ListTemplateId="100">
<Receiver>
<Name>EventReceiver1ItemAdding</Name>
<Type>ItemAdding</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>CustomEventReceiver.EventReceiver1.EventReceiver1</Class>
<SequenceNumber>10000</SequenceNumber>
</Receiver>
</Receivers>
</Elements>
  Receiver通常包含五个子节点:
  1、Name:定义唯一名字信息;
  2、Type:定义一个事件类型,如果添加新的事件类型,需要再添加一个Receiver子节点;
  3、Assembly:定义sharepoint程序集清单;Sharepoint Assembly
  4、Class:包含带命名空间的事件处理器类的类名;
  5、SequenceNumber:如果有多个事件处理器绑定到同一个列表的时候通过此节点值判断执行顺序
  
  如果希望值绑定在某个特定的列表,可以通过修改以下内容:
  <Receivers ListUrl="Lists/列表名">
  
  通过添加data标签,可以像事件处理器传递少量数据信息:


View Code


<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Receivers ListTemplateId="100">
<Receiver>
<Name>EventReceiver1ItemAdding</Name>
<Type>ItemAdding</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>CustomEventReceiver.EventReceiver1.EventReceiver1</Class>
<SequenceNumber>10000</SequenceNumber>
<Data> Data information<Data>
</Receiver>
</Receivers>
</Elements>
  读取方法:
Stringdata=properties.ReceiverData;
  
  三、事件绑定
  方法一:Feature绑定
  在Element.xml文件中定义内容,部署。
  缺陷:通过XML配置,无法将内容类型和事件处理器进行绑定,而通过对象模型的方式可以。
  
  方法二:用sharepoint对象模型进行绑定
  参考代码:


View Code


Type receiverType = typeof(事件查看器类);       //typeof取得系统对类的描述
using (SPSite site = new SPSite("http://localhost/sites"))
{
SPWeb web = site.RootWeb;
SPList list = web.Lists.TryGetList("列名");
SPEventReceiverDefinitionCollection receiverCol = list.EventReceivers;
SPEventReceiverDefinition recevierDef = receiverCol.Add();
recevierDef.Assembly = receiverType.Assembly.FullName;
recevierDef.Class = "CustomEventReceiver.EventReceiver1.EventReceiver1";
recevierDef.Type = SPEventReceiverType.ItemAdding;
recevierDef.SequenceNumber = 10000;                     
recevierDef.Update();
}
  在绑定前要做个判断,确保同一个事件处理器没有和相应的列表绑定,加入如下代码:


View Code


SPEventReceiverDefinitionCollection receiverCol = list.EventReceivers;
foreach (SPEventReceiverDefinition def in receiverCol)
{
if (def.Assembly == receiverType.Assembly.FullName)
{
def.Delete();
break;         
}
}
  
  四、其他应用
  After事件:注册同步After事件的方法(默认为异步):
  1、XML绑定


View Code


<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Receivers ListTemplateId="100">
<Receiver>
<Name>EventReceiver1ItemAdding</Name>
<Type>ItemAdding</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>CustomEventReceiver.EventReceiver1.EventReceiver1</Class>
<SequenceNumber>10000</SequenceNumber>
<Synchronization>Synchronous</Synchronization>
</Receiver>
</Receivers>
</Elements>
  2、SharePoint对象模型


View Code


Type receiverType = typeof(事件查看器类);       //typeof取得系统对类的描述
using (SPSite site = new SPSite("http://localhost/sites"))
{
SPWeb web = site.RootWeb;
SPList list = web.Lists.TryGetList("列名");
SPEventReceiverDefinitionCollection receiverCol = list.EventReceivers;
foreach (SPEventReceiverDefinition def in receiverCol)
{
if (def.Assembly == receiverType.Assembly.FullName)
{
def.Delete();
break;                //删除后需要加上break,否则foreach会在删除后报错

}
}
SPEventReceiverDefinition recevierDef = receiverCol.Add();
recevierDef.Assembly = receiverType.Assembly.FullName;
recevierDef.Class = "CustomEventReceiver.EventReceiver1.EventReceiver1";
recevierDef.Type = SPEventReceiverType.ItemAdding;
recevierDef.SequenceNumber = 10000;                  
recevierDef.Synchronization=SPEventReceiverSynchronization.Synchronous;
recevierDef.Update();
}

  避免二次触发引起的无限循环调用(如调用Update方法修改数据后继续调用Update方法,一直到资源超限),使用EventFiringEnabled属性:



public override void ItemUpdated(SPItemEventProperties properties)
{
this.EventFiringEnabled = false;
properties.ListItem["Title"] = "Title";
properties.ListItem.Update();
this.EventFiringEnabled = true;
}
  如果在更新有版本控制的列表项时不希望更新版本,则调用SPListItem的另一个Update方法:
  properties.ListItem.UpdateOverwriteVersion();
  
  SPListItem还有另外一种更新方式SystemUpdate(),可以再更新数据时不触发其他关联字段。
  MSDN解释:
  Updates the database with changes that are made to the list item without changing the Modified or Modified By fields.
  SystemUpdate方法有两个重载,带有bool参数的重载可以指定是否生成新的版本。


View Code


       public override void ItemUpdated(SPItemEventProperties properties)
{
this.EventFiringEnabled = false;
properties.ListItem["Title"] = "Title";
properties.ListItem.SystemUpdate();
this.EventFiringEnabled = true;   
}
  
  
页: [1]
查看完整版本: SharePoint事件处理器