tags:

views:

309

answers:

1

how can i redirect to a page from sharepoint?

i have this within the itemadded event receiver for a list:

SPUtility.Redirect("http://mysite", SPRedirectFlags.Default, HttpContext.Current );

but on debugging HttpContext.Current is null so it doesnt do any redirect when a list item is added.

+5  A: 

The ItemAdded event occurs asynchronously (i.e. sometime after the item is added). It's executing on a separate thread that doesn't have access to the HttpContext for the current request (and therefore you won't be able to send a response to tell the user's browser to redirect).

This might work in the ItemAdding event receiver - it executes on the same thread that adds the item to a the list. I'm not sure that it's safe to perform a redirect since it might prevent other underlying code to be executed in SharePoint.

This was echoed by Lars Fastrup in one of your previous questions (redirect using itemreceiver sharepoint). Consider implementing the solution he recommends there.

dariom
perfect answer. Seeing as ItemEventReceivers occur Asynchronously, it could theoretically be possible that a user gets redirected after say 30 seconds while he is already doing something else. Imagine his face when all of a sudden he finds himself on a different page :-P
Colin