Working with BeforeProperties and AfterProperties on SPItemEventReceiver
As many of you know, event receivers are a great way to hook into various SharePoint events. These can apply to Feature events such as FeatureActivated, List events such as FieldAdded, and many others. The most common set of receivers used, however, are part of SPItemEventReceiver which let you wire your code up to a number of events that can occur to items on a list or library.
When working with events, you’ll quickly find that before (synchronous) and after (asynchronous) events exist, and the method suffix such as “ing” (e.g. ItemAdding) and “ed” (e.g. ItemAdded) will tell you whether it gets invoked before or after the actual change is made. Basic stuff.
And, as you get deeper, you’ll even find that you can extract the before and after state of the change. For example, you can hook into the ItemUpdating event for a document library and prevent a user from changing a certain column. The code might look like this:
public override void ItemUpdating(SPItemEventProperties properties)
{
if (properties.BeforeProperties["column"] != properties.AfterProperties["column"])
{
properties.Cancel = true;
properties.ErrorMessage = "This column cannot be changed";
}
}
For a document library, this works just fine. However, you should know that the BeforeProperties hash table is not populated for items on a list. As is worded in the SDK: “For documents, Before and After properties are guaranteed for post events, such as ItemUpdated, but Before properties are not available for post events on list items”
When they say “not available for post events on list items”, do they mean after events (like ItemUpdated, ItemDeleted, etc)? The wording is curious here, so I thought I’d take some time to test each combination of common events such as Add, Update and Delete. These were done across a custom list and then a document library. Each test involved adding a new item, editing the item and then deleting the item. Here are the results for a list:
List | BeforeProperties | AfterProperties | properties.ListItem |
ItemAdding | No value | New value | Null |
ItemAdded | No value | New value | New value |
ItemUpdating | No value | Changed value | Original value |
ItemUpdated | No value | Changed value | Changed value |
ItemDeleting | No value | No value | Original value |
ItemDeleted | No value | No value | Null |
No value means that column value in the hash table was not available.
New value means that the correct value for the column was available.
Changed value means that the correct updated value was available.
Original value means that the correct original value was available.
Here is the same test against a document library:
Library | BeforeProperties | AfterProperties | properties.ListItem |
ItemAdding | No value | No value | Null |
ItemAdded | No value | No value | New value |
ItemUpdating | Original value | Changed value | Original value |
ItemUpdated | Original value | Changed value | Changed value |
ItemDeleting | No value | No value | Original value |
ItemDeleted | No value | No value | Null |
Properties.ListItem refers the the current value for the list item at that point in the event. Null means that the item is not available. My analysis yields the following results:
- Not surprisingly, we get null values for for ItemAdding (before item is added) and ItemDeleted (after item is deleted). This was proven by Ishai Sagi some time ago.
- As correctly documented in the SDK, item events for lists do not expose BeforeProperties.
- ItemAdding and ItemAdded correctly report the value in the AfterProperties for an list item, but not a library item. This is curious.
- We have no visibility on the previous states during the ItemDeleted event. Once it’s deleted, it’s clearly gone.
So, if we go back to our original problem listed above. How can we prevent a user from changing a certain column for an item in a list event? From the list table, you can see if we hook into the ItemUpdating event, we can compare the current item’s value (properties.ListItem) to the AfterProperties value. The code would look like this:
if (properties.ListItem["column"] != properties.AfterProperties["column"])
{
properties.Cancel = true;
properties.ErrorMessage = "This column cannot be changed";
}
I hope this post gives you a better idea of how before and after events work for both lists and libraries. Your comments and feedback are always welcomed.
Technorati: SharePoint
For the latest news, tips and tricks from Synergy Team in SharePoint Development World, please check out our SharePoint Development Blog Library.
原文地址:http://www.synergyonline.com/Blog/Lists/Posts/Post.aspx?ID=122
Working with BeforeProperties and AfterProperties on SPItemEventReceiver的更多相关文章
- 在SPItemEventReceiver中使用BeforeProperties和AfterProperties
当你利用这些事件时,就很快会发现存在前(同步)后(异步)两种事件.其方法的后缀分别为“ing”(比如,ItemAdding)和“ed”(比如,ItemAdded),分别代表了变更发生前调用和发生后调用 ...
- BeforeProperties/AfterProperties in Event Receivers
Sharepoint List List BeforeProperties AfterProperties properties.ListItem ItemAdding No Value No Val ...
- 关于sharepoint事件接收器中properties.AfterProperties[""].Tostring()取值的问题。
这个这个属性是不能获取到中文的意思,他是获取AfterProperties的集合的值. string name=properties.AfterProperties["登录人"]. ...
- SharePoint 2013 状态机工作流之日常报销示例
简单介绍下状态机工作流,状态机工作流提供了一系列的状态.工作流从初始状态开始,到终止状态结束.两个状态之间定义行为进行过渡.通常情况下,状态机工作流对事件作出反应,事件的发生将会使状态发生改变. 1. ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q73-Q76)
Question 73You create a Web Part that calls a function named longCall.You discover that longCall tak ...
- Creating a SharePoint Sequential Workflow
https://msdn.microsoft.com/en-us/library/office/hh824675(v=office.14).aspx Creating a SharePoint Seq ...
- SharePoint暂时禁用事件触发
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsof ...
- SpringRMI解析2-RmiServiceExporter逻辑脉络
配置文件是Spring的核心,在配置文件中我们可以看到,定义了两个bean,其中一个是对接口实现类的发布,而另一个则是对RMI服务的发布,使用org.springframework.remoting. ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q104-Q106)
Question 104You plan to create a workflow that has the following three activities: CreateTask OnTask ...
随机推荐
- BZOJ_1600_[Usaco2008_Oct]_建造栅栏_(动态规划)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1600 将长度为n的线段分成4段,长度为整数,围成面积>0的四边形,求方案数. 分析 首先 ...
- SharePoint 2010 部署 WSP 包
转:http://www.cnblogs.com/myheaven/archive/2011/05/19/2051180.html .net在工程的下面生成了WSP包,在Debug下面.需要放到生产环 ...
- Lisp语言学习的书
Scheme <How to Design Programs : An Introduction to Programming and Computing>(<程序设计方法>) ...
- 一步一步学Remoting系列文章
转自:http://www.cnblogs.com/lovecherry/archive/2005/05/24/161437.html (原创)一步一步学Remoting之一:从简单开始(原创)一步一 ...
- diamond专题(四)—— 容灾机制
大家好,本次为大家带来diamond的容灾机制. diamond之所以表现的稳定可靠,除了架构简单之外,另一个重要原因是diamond具有一套完备的容灾机制,容灾机制涉及到client和server两 ...
- imdisk命令行使用及配置
imdisk是一个开源的虚拟磁盘软件,集虚拟光驱,文件虚拟光驱,映射物理磁盘,映射物理内存等功能 如果使用devio--Device I/O Service,可以映射网络磁盘等. 通用于windows ...
- 【原】泛型-Java
泛型是Java SE5.0中新增的新特性,通过泛型使得在代码编译的时候就能检查类型安全,并且所有的强制类型转换都是自动和隐式的,从而提高代码的重用率. Note:在JavaSE7+以后的版本中,构造函 ...
- Java笔记(十六)……内部类
内部类概述 内部类是将一个类定义在另一个类里面,对里面那个类就成为内部类(内部类,嵌套类). 当描述事物时,事物的内部还有事物,该事物用内部类来描述,因为内部事物在使用外部事物的内容 访问特点 内部类 ...
- eclipse tomcat内存溢出,加大内存
保存图片失败,请点击这里获得详细信息. 加入 -Xms256M -Xmx512M -XX:PermSize=256m -XX:MaxPermSize=512m
- bzoj3998 [TJOI2015]弦论(SAM)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3998 [题意] 询问排名第k的子串是谁,0代表相同子串不同位置算作相同,1代表相同子串 ...