博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]SP 2010: How To – Event Receivers and Custom Error Pages
阅读量:4574 次
发布时间:2019-06-08

本文共 3528 字,大约阅读时间需要 11 分钟。

原文地址:

 

SharePoint 2010

The object model has been extended in various places, and this is one of the most welcome changes for me and a lot of my fellow peers doing daily SharePoint development.

So, in this article I will walk you through the news with Event Receivers in SP 2010 in regards to creating custom Error Pages for your users.

Creating an Event Receiver in SharePoint 2010 with a Custom Error Page

Using Visual Studio 2010, choose the new template called "Event Receiver" like so:

Choose if you want this to be a Farm Solution or a Sandboxed Solution. Then click next:

Finally, choose the Type of receiver you want to create, what event you would like to hook up and to what type. I chose "List Item Events", "Announcements" and "An item is being added":

Click next and let Visual Studio 2010 work it’s magic.

You’re presented with the following project structure that is created for us:

I will not dig deep on how and why the structure of the project looks the way it does now – it will be covered in another article.

1. Adding some basic logic for your Event Receiver

In the EventReceiver1.cs file that you’re presented with, you can quite easily add any code you want – and some code has already been added so you don’t have to!

The code looks like this out of the box:

Now, what I want to do in order to make sure my event receiver works – is to simply add some dummy-code and have it tested!

Add the following code to your ItemAdding-method:

All we do here is check our item that is being added makes a condition to see if the DueDate property is set. The breakpoint is simply added because you easily should see that your code executes and works as expected.

2. Press the Holy Button (F5)

By pressing F5, Visual Studio will take care of the build, packaging and deployment of your Event Receiver. For more details on the actual F5-experience, I encourage you to read MSDN, SDK and all the blogs out there.

You are presented with a web page (VS 2010 launches IE for you as well), where you now can easily test your Event Receiver.

In our case, when adding a new Announcement, we should automatically check the DueDate property. Currently we don’t cancel the event or do anything else – let’s leave that to your imagination.

Cancelling an Event in SharePoint 2010

So – now that we’ve got a very basic Event Receiver in SharePoint 2010, we should add that Custom Error Page we talked about.

  • Start by adding a New Item to your project of type Application Page:
  • Visual Studio 2010 will now automatically add a mapped folder called "Layouts" which is mapping to the _layouts folder in the SharePointRoot (14-folder):
  • Edit the ErrorDueDate.aspx file to add some rich HTML:
    Note that I am referring to an image as well. I just popped that into the same Layouts-folder that was created for my Event Receiver project.
  • Edit your EventReceiver1.cs file and add the current logic:
    This will now cancel the event if the DueDate field is empty, and show the user a custom error page that you designed yourself.
  • Hit F5 and let Visual Studio 2010 work it’s magic again
  • Try it out by adding a new Announcement without adding a DueDate:
  • You should now be presented with the following dialog:

As you can see, your custom HTML now appears. Apparently I didn’t do any fancy design on my Application Page, but you can add more images and whatever else you want to make it more easy for the users to understand what actually went wrong – and how to make it on from there

转载于:https://www.cnblogs.com/Roy_Cao/archive/2012/10/22/2733423.html

你可能感兴趣的文章
visual studio 2017 使用笔记
查看>>
iTerm2 半透明颜色主题与字体配置
查看>>
《Entity Framework 6 Recipes》中文翻译系列 (14) -----第三章 查询之查询中设置默认值和存储过程返回多结果集...
查看>>
asp.net后台InputStream存储图片,前台js接收解析存放路径地址
查看>>
双飞翼布局和圣杯布局
查看>>
java并发编程之CountDownLatch与CyclicBarrier
查看>>
AOJ 0525 - Osenbei
查看>>
iOS 直播-闪光灯的使用
查看>>
关于 Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))的问题
查看>>
python数据类型之间的转换
查看>>
[T-ARA][I'm so bad]
查看>>
win7,win10获取屏幕缩放适应截图
查看>>
MySQL常用命令
查看>>
python3实现合并两个有序数组
查看>>
InventTrans中的状态跟踪
查看>>
python flsak 框架
查看>>
h5页面调起微信支付
查看>>
loadrunner中pacing设置01
查看>>
python 选课系统
查看>>
C语言复习: 二级指针和多级指针
查看>>