MOSS 2010:Visual Studio 2010开发体验(14)——列表开发之事件接收器
来源:互联网 发布:西安java讲师 编辑:程序博客网 时间:2024/04/30 11:19
通过前面几篇,我们已经完成了内容类型,列表定义,列表实例的开发。本篇继续讲解列表中的一个重要环节——事件接收器开发。
我们的场景是:我希望之前做好的订单列表这个内容类型自动地具有某些事件特征,例如当用户在添加一个条目的时候,检查订购日期,如果小于今天的话,就不让添加。(这只是一个假设的场景,现实工作中可以依照业务逻辑而定)
我们应该如何实现这个需求呢?Follow me
1.添加一个事件接收器
下面是默认生成的两个文件
2. 修改有关代码实现业务逻辑
using System;using System.Security.Permissions;using Microsoft.SharePoint;using Microsoft.SharePoint.Security;using Microsoft.SharePoint.Utilities;using Microsoft.SharePoint.Workflow; namespace OrderListSolution.OrderItemEventReceiver{ ////// List Item Events /// public class OrderItemEventReceiver : SPItemEventReceiver { /// /// An item is being added. /// public override void ItemAdding(SPItemEventProperties properties) { SPItemEventDataCollection data = properties.AfterProperties; SPList list = properties.OpenWeb().Lists[properties.ListId]; string fieldName= list.Fields.GetField("订购日").InternalName; var date = DateTime.Parse(data[fieldName].ToString()); if (date > DateTime.Now) { properties.ErrorMessage = "当前添加的日期不合法"; properties.Cancel = true; return; } } }}
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }
3.部署和调试,按下F5键
【注意】我们这里故意将订购日设置于大于当前日期。根据事件中的逻辑,我们预期它会报告一个错误,而且取消当前的提交
我们看到了这个错误页面,看起来有些吓人,不是吗?对于一般用户来说,他们可能要被吓坏了
接下来,我们可以定义一个专门的页面来显示错误消息,提高用户体验
然后修改一下代码
using System;using System.Security.Permissions;using Microsoft.SharePoint;using Microsoft.SharePoint.Security;using Microsoft.SharePoint.Utilities;using Microsoft.SharePoint.Workflow;namespace OrderListSolution.OrderItemEventReceiver{ ///.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }/// List Item Events /// public class OrderItemEventReceiver : SPItemEventReceiver { /// /// An item is being added. /// public override void ItemAdding(SPItemEventProperties properties) { SPItemEventDataCollection data = properties.AfterProperties; SPList list = properties.OpenWeb().Lists[properties.ListId]; string fieldName= list.Fields.GetField("订购日").InternalName; var date = DateTime.Parse(data[fieldName].ToString()); if (date > DateTime.Now) { //properties.ErrorMessage = "当前添加的日期不合法"; properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl; properties.RedirectUrl = "/_layouts/OrderListSolution/ErrorPage.aspx"; properties.Cancel = true; } } }}
- MOSS 2010:Visual Studio 2010开发体验(14)——列表开发之事件接收器
- MOSS 2010:Visual Studio 2010开发体验(14)——列表开发之事件接收器
- MOSS 2010:Visual Studio 2010开发体验(12)——列表开发之列表定义
- MOSS 2010:Visual Studio 2010开发体验(13)——列表开发之列表实例
- MOSS 2010:Visual Studio 2010开发体验(10)——列表开发之内容类型
- MOSS 2010:Visual Studio 2010开发体验(9)—列表开发之基本概念
- MOSS 2010:Visual Studio 2010开发体验(31)——如何给列表绑定多个内容类型
- MOSS 2010:Visual Studio 2010开发体验(6)——开发WebPart
- MOSS 2010:Visual Studio 2010开发体验(26)——工作流开发概述
- MOSS 2010:Visual Studio 2010开发体验(1)——SharePoint Explorer
- MOSS 2010:Visual Studio 2010开发体验(2)——项目模板和结构
- MOSS 2010:Visual Studio 2010开发体验(4)——应用程序页面(ApplicationPage)
- MOSS 2010:Visual Studio 2010开发体验(5)——Mapped Folder
- MOSS 2010:Visual Studio 2010开发体验(7)——AJAX Web Part
- MOSS 2010:Visual Studio 2010开发体验(8)——Silverlight应用
- MOSS 2010:Visual Studio 2010开发体验(11)——扩展SharePoint Explorer
- MOSS 2010:Visual Studio 2010开发体验(15)——LINQ to SharePoint
- MOSS 2010:Visual Studio 2010开发体验(16)——客户端对象模型
- MOSS 2010:Visual Studio 2010开发体验(12)——列表开发之列表定义
- 精典的SQL语句
- 静态方法与非静态方法
- Android 屏幕旋转时保存状态
- MOSS 2010:Visual Studio 2010开发体验(13)——列表开发之列表实例
- MOSS 2010:Visual Studio 2010开发体验(14)——列表开发之事件接收器
- Excel:如何使用函数实现多表多条件汇总求和
- C语言实现直接插入排序
- MOSS 2010:Visual Studio 2010开发体验(15)——LINQ to SharePoint
- 表分区的概念
- 关于本博客的一些使用说明
- MOSS 2010:Visual Studio 2010开发体验(16)——客户端对象模型
- Office 2010的一个小问题
- MOSS 2010:Visual Studio 2010开发体验(17)——在Silverlight应用程序中访问客户端对象模型