C# – redirect code not working in itemreceiver

csharepoint

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.

Best Answer

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.