Redirecting User on ItemAdding in SharePoint EventHanlders

Here is an issue from my colleague.

The problem

We got a custom list for which handling ItemAdding Event, in the ItemAdding Event (We are creating a site). We are trying to redirect user to a custom site page, the redirection works fine but the item doesn’t gets added to the list at the same time the site gets created.  On ItemAdded, we are trying the redirection code but HttpContext is null

The Solution

The reason why our item isn’t added is when its redirect, the current thread is aborted and the item is never added to the list. We will have to manually add the item.
HttpContext is null in the ItemAdded because this event is asynchronous – The HttpContext is only available within the constructor when a synchronous event is fired.

The following article explains the same problem and provides a details solution:
http://www.entwicklungsgedanken.de/2008/03/27/redirecting-from-newformaspx-to-dispformaspx-after-creating-a-new-item/

* Thanks to Elin and Akhilesh!

Posted in MOSS, SharePoint.

Leave a Reply