0

I'd like to know if there are a way to make a redirection in an event receiver of a list itemUpdated and not updating. I need this because in my event, i create a file in a folder and of a document library and i want to redirect the user to this new item.

1

I don't think so. ItemUpdated is asynchronous, so by the time it fires, a response has already been sent back to the browser. If you do not have access to the HTTP Request and Response, you can't tell the browser to redirect.

0

As the processing of the after event also fires on a different thread it is also not possible to redirect to a different page from within the after event as neither the HttpContext object from the request initiating the original operation is available nor is it guaranteed that the after event is executed before the response of the original request has been sent back to the browser.

To overcome this limitation SharePoint has additional functionality to allow synchronous execution of after events. The default is still asynchronous but you can now configure your after event explicitly to execute synchronous.

The easiest way to achieve this is to modify the elements.xml file of the feature which registers the event receiver as outlined in the following article.

Example:

EventReceiver1WebDeleted
WebDeleted
$SharePoint.Project.AssemblyFullName$
MyEventLogger.MyEventReceivers.EventReceiver1
10000
Synchronous

In case that the event receiver is registered programmatically you can achieve the same result by setting the Synchronization property of the SPEventReceiverDefinition object to SPEventReceiverSynchronization.Synchronous

Source

Once completed, to get the redirect functioning, you will need to do the following to get access to the Http Context in your ItemUpdated (for additional details see this blog).

  1. Create two private global variables in your event receiver:

    private HttpContext _httpContext; private static HttpContext _sHttpContext;

  2. Get Current Http Context in the constructor

    _httpContext = HttpContext.Current;

  3. In ItemUpdating, again get a hold of the HttpContext

    _sHttpContext = _httpContext

  4. Use SPUtility.Redirect and _sHttpContext to redirect to a page of your choice.

  • Thanks for your proposition but in my case, it doesn't redirect at all. Nothing happened, i write uls log after redirect and it's correctly writed. Did you have this issue? – user1898765 Apr 20 '15 at 17:50
  • Could you try the second portion, without the "synchronous" flag toggled? I'm unfortunately at the loss of a VM, to validate the changes, but read from the two sources the approaches, which appeared to be functioning. If either functions, independently, let me know and I'll update the solution. – Quinn Johns Apr 21 '15 at 2:44
  • I added synchronous on every event (addind, added, updating and updated) i ll test tonight and come back with more details – user1898765 Apr 21 '15 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.