设为首页 收藏本站
查看: 951|回复: 0

[经验分享] SharePoint事件处理器

[复制链接]

尚未签到

发表于 2015-9-25 00:43:37 | 显示全部楼层 |阅读模式
  以下内容是对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为例):


DSC0000.gif DSC0001.gif 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>
  读取方法:
String  data=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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-118300-1-1.html 上篇帖子: SharePoint 2010 技巧: 限制People Picker搜索非站点集内的用户 下篇帖子: Sharepoint学习笔记---Linq to Sharepoint--查询语法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表