3
votes

Anybody used the "ItemAdding" method successfully in an Event Receiver in Sharepoint for validation?

I think it just can't work properly for validation purposes because it seems that :

  • You can't access the data you just entered in a form
  • If you add an error message, instead of displaying the form back with your error, Sharepoint
    display the big ugly error page.

Here's what I found so far on the web for the ItemAdding method :

http://www.sharepoint-tips.com/2006/09/synchronous-add-list-event-itemadding.html

http://www.moss2007.be/blogs/vandest/archive/2007/07/20/wss-3-0-event-handler-pre-event-cancelling-issues.aspx

http://www.synergyonline.com/blog/blog-moss/Lists/Posts/Post.aspx?ID=25

Anyway, if somebody successfully used that method, please tell me how because I think that it's just broken/missing something!

Thanks!

2

2 Answers

6
votes

You can't use ItemAdding to perform that kind of "friendly" validation checking. If you set e.Cancel = true and set e.ErrorMessage, sharepoint will redirect you to the standard error page and display your message. You can't make it redirect back to your form page.

Rather than trying to hack the built in pages, you should use an infopath or aspx form bound to a content type. Infopath has built-in validation controls and with aspx forms you build yourself you can treat it like any web app and choose your own validation.

No easy way out here I'm afraid.

-Oisin

4
votes

You can access data that was just added in the form. Look at properties.AfterProperties in the adding/updating event receivers.

An example would be properties.AfterProperties["Title"].

I agree that error handling is a little hard, but I recommend that you do validation in your event receivers in addition to having your UI do validation. If it is not a custom page, then you can add JavaScript to existing pages using a Content Editor Web Part to do the validation.