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 ...
随机推荐
- easyui datagrid plunges 扩展 插件
项目使用 springmvc4.x spring4.x hibernate4.x easyui 为了便于开发,扩展了easyui 的 datagrid 功能,下面直接贴上扩展代码: /** * ...
- CDOJ 1071 秋实大哥下棋 线段树
分析:运用扫描线,先从左到右扫描,用纵坐标进行建树, 随着扫描线的右向右移动.不断更新横坐标小于扫描线的车 更新的时候 在树中更新车的纵坐标的位置,把该位置的值变成该车的横坐标 线段树维护的是区间最 ...
- 浅谈PetShop之使用存储过程与PLSQL批量处理(附案例)
1 大概思路 备注:黄色为影响参数 2 PetShop4的经典数据库连接代码回顾 PetShop4有3个函数,具体有: ExecuteReader:可以读一个表的记录,只能读不 ...
- PHP获取上个月、下个月、本月的日期
获取本月日期: 代码如下: function getMonth($date){ $firstday = date("Y-m-01",strtotime($date)); $la ...
- Windows Azure 的磁盘管理相关概念
在 Windows Azure 的虚拟机中,磁盘有多种使用方式.操作系统磁盘是用来为虚拟机提供操作系统的虚拟硬盘.数据磁盘是附加到虚拟机上用来存储应用程序数据的 VHD. 根据应用程序的需要,可从多种 ...
- Weka 入门2
现在我们介绍使用Weka来对数据进行分类.对数据进行分类,我们必须先指定那一列作为预测类别.因为数据文件格式的问题,类别一般都是最后一列属性.我们可以使用setClassIndex来设置类别.然后我们 ...
- java 关于extends 和implement的区别
在java中extends用于继承父类,只要父类不是声明为final或者为abstract类就可以,但是java不支持多重继承.可以使用接口实现多重继承implements,继承只能继承一个类,但im ...
- ASP.Net IE10 _doPostBack 未定义错误【转】
--昨天发现IE10下面ReportViewer执行报表会报错,发现为Js报_doPostBack 未定义错误,查找相关资料发现问题为 当前framework不能识别IE10版本,把该浏览器做降级处理 ...
- Markdown中实现缩进的方法
markdown中实现缩进的方法 在每一行开头的时候,先输入下面的代码,然后紧跟着输入文本即可.注意有分号. 半角空格: 或 全角空格: 或 不换行空格: 或
- 动态获取jar文件的路径
下面专门封装了一个类来处理: import java.io.File; /** * 获取打包后jar的路径信息 * @author Administrator * 2011-01-16 13:53 ...