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 ...
随机推荐
- Hibernate: org.hibernate.exception.SQLGrammarException: could not insert: 错误
最近在学习Java Web,今天刚接触Hibernate.学习的书籍是<轻量级Java EE企业应用实战(第3版)>.书中367页5.2.2中给予的Hibernate例子中的代码运行有以下 ...
- ☀【JS】检测属性
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- Charles抓Https的包
1: 手机上打开这个地址, 安装文件. http://www.charlesproxy.com/getssl 2:保证手机与电脑在同一个网段 3:按住alt再点击wifi, 显示出当前电脑的ip地址. ...
- log4net面面观之Repository
转:http://itrust.cnblogs.com/archive/2006/07/17/452895.html 上回说道:Repository可以说成基于一个log4net配置节创建的log4n ...
- 转载--配置WAMP开发环境
转自:http://www.cnblogs.com/cardon/archive/2009/12/13/1622935.html 本例安装文件在这里下载 apache2.2.4 MySQL ...
- vs212创建mvc3项目,添加ADO.NET实体数据模型时产生 XXXX.Desiger.cs 文件为空
vs212创建mvc3项目,发现添加ADO.NET实体数据模型时,产生StoreDB.Desiger.cs文件为空 产生StoreDB.Desiger.cs文件为空 原因是,在vs2012中,添加AD ...
- C# 中判断程序是否启动使用Mutex使用异常
[背景] 在最近的一个项目中,我负责客户端模块的工作.需求要求,在启动客户端时需要判断客户端是否已经启动(单例).于是我决定使用Mutex来实现此功能,代码如下: bool initiallyOwne ...
- HW3.13
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- UVa11404 - Palindromic Subsequence(区间DP+打印路径)
题目大意 给定一个字符串,要求你删除尽量少的字符,使得原字符串变为最长回文串,并把回文串输出,如果答案有多种,则输出字典序最小的 题解 有两种解法,第一种是把字符串逆序,然后求两个字符串的LCS,并记 ...
- ios iphone 将log在终端输出
对于模拟器,其在终端的log文件位于: -/Library/Logs/CoreSimulator/C4B94BA6-EF08-4AD2-AE7D-1A3A2E2AC545/system.log 对 ...