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的更多相关文章

  1. 在SPItemEventReceiver中使用BeforeProperties和AfterProperties

    当你利用这些事件时,就很快会发现存在前(同步)后(异步)两种事件.其方法的后缀分别为“ing”(比如,ItemAdding)和“ed”(比如,ItemAdded),分别代表了变更发生前调用和发生后调用 ...

  2. BeforeProperties/AfterProperties in Event Receivers

    Sharepoint List List BeforeProperties AfterProperties properties.ListItem ItemAdding No Value No Val ...

  3. 关于sharepoint事件接收器中properties.AfterProperties[""].Tostring()取值的问题。

    这个这个属性是不能获取到中文的意思,他是获取AfterProperties的集合的值. string name=properties.AfterProperties["登录人"]. ...

  4. SharePoint 2013 状态机工作流之日常报销示例

    简单介绍下状态机工作流,状态机工作流提供了一系列的状态.工作流从初始状态开始,到终止状态结束.两个状态之间定义行为进行过渡.通常情况下,状态机工作流对事件作出反应,事件的发生将会使状态发生改变. 1. ...

  5. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q73-Q76)

    Question 73You create a Web Part that calls a function named longCall.You discover that longCall tak ...

  6. Creating a SharePoint Sequential Workflow

    https://msdn.microsoft.com/en-us/library/office/hh824675(v=office.14).aspx Creating a SharePoint Seq ...

  7. SharePoint暂时禁用事件触发

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsof ...

  8. SpringRMI解析2-RmiServiceExporter逻辑脉络

    配置文件是Spring的核心,在配置文件中我们可以看到,定义了两个bean,其中一个是对接口实现类的发布,而另一个则是对RMI服务的发布,使用org.springframework.remoting. ...

  9. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q104-Q106)

    Question 104You plan to create a workflow that has the following three activities: CreateTask OnTask ...

随机推荐

  1. PHP位运算 详细说明

    在实际应用中可以做用户权限的应用我这里说到的权限管理办法是一个普遍采用的方法,主要是使用到”位运行符”操作,& 位与运算符.| 位或运行符.参与运算的如果是10进制数,则会被转换至2进制数参与 ...

  2. boost总结之any

    boost中any库相对variant简单,any可以不限定类型,variant中对我们事先会定义好我们所需的类型,但是any无此限制,any的类型检测是在run time时.   boost::an ...

  3. Cocos2d-x ios 下http请求的另一种实现

    简单描述下需求:游戏要加入事件log,比如玩家升到10级:创建角色:或是,触发这些事件后要求客户端忘后台抛送一条log信息.一般情况下,我们可以直接使用cocos自带的HttpClient(底层用li ...

  4. 【原】Arrays.binarySearch() 的用法

    Arrays.binarySearch() 的用法 1.binarySearch(Object[] a, Object key) Searches the specified array for th ...

  5. 揭开嵌入式c面试题背后的玄机

    今天老大让我针对一个面试者出些嵌入式方面的面试题,主要是想对他的技术深度进一步了解.我就出了下面这些问题,每个问题背后都是考察一个嵌入式程序员应该具备的相关技能.当然这些只是我的个人理解,不一定正确. ...

  6. MVC 5 第三章 HTML Helper

    提及到HTML helper大家肯定不应该陌生, 因为在书写MVC View的时候肯定需要使用到它.一个HTML Help就是一个返回HTML字符串的方法,这个字符串表示你所期望的类型的内容.例如,你 ...

  7. 【Java基础】成员变量和局部变量的区别

    在类中的位置不同 成员变量:在类内部方法外部 局部变量:在方法体内部定义的或者方法的参数中定义的在内存中的位置不同 成员变量:在堆内存,有初始化值,byte,short,int,long->0, ...

  8. Excel中VBA 连接 数据库 方法- 摘自网络

    Sub GetData() Dim strConn As String, strSQL As String Dim conn As ADODB.Connection Dim ds As ADODB.R ...

  9. console.read()读入的内容

    今天写的特别简单的代码,大体是一个模式选择,从控制台读入一个数,然后做出相应的选择. 代码如下: using System; using System.Collections.Generic; usi ...

  10. 操作系统课堂笔记(2)操作系统的硬件环境之I/O技术和时钟

    I/O技术 1.程序控制I/O技术 有处理器提供相关的IO指令来实现的.主要缺陷是,处理器必须关注IO处理单元的状态,因而它会耗费大量的时间轮询以获得这个信息,这严重降低了系统性能. 2.中断驱动I/ ...