0

I want to redirect to another page after an item is added to the list. I have referred many links and found the same alternative that is

      SPUtility.Redirect("https://www.google.co.in/", Microsoft.SharePoint.Utilities.SPRedirectFlags.Default, context);

But this does not work for me. What can I do to achieve this requirement?

3 Answers 3

5

Unfortunately redirecting after an item is added isn't easy.

The event receivers only allow redirecting when it's a Pre event like ItemAdding by the use of SPEventReceiverStatus.CancelWithRedirectUrl, but this cancels the adding of the item.

So in order to redirect when the add is done you have to:

  1. Implement your logic in ItemAdding
  2. Retrieve all the values from SPItemEventProperties.AfterProperties
  3. Disable Events by setting SPEventReceiverBase.EventFiringEnabled
  4. Add the item using code
  5. Set SPItemEventProperties.RedirectUrl
  6. Set SPItemEventProperties.Status to CancelWithRedirectUrl
2

Try to use

properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
properties.RedirectUrl = "https://www.google.co.in/";
2

While @alexander and @per-jakobsen have the correct answers I would like to add an alternative approach, the hackish one:

private readonly HttpContext _context;

public YourEvent()
{
    _context = HttpContext.Current;
}

public override void ItemUpdating(SPItemEventProperties properties)
{
    base.ItemUpdating(properties);
    _context.Response.Status = "302 Moved Temporary";
    _context.Response.AddHeader("Location", "http://google.com");
}

Note _context.Response.Redirec(..) will not work, and this method might not work in SharePoint 2013.

4
  • The value of HttpContext.Current obtained is always null and this causes exception. How can I get the context in an event receiver? Commented Sep 23, 2013 at 13:11
  • SharePoint 2013?
    – eirikb
    Commented Sep 23, 2013 at 13:14
  • Yes I am working in SharePoint 2013. Commented Sep 24, 2013 at 7:18
  • I'm afraid this solution does not work in 2013. It might work for other events though, such as FieldUpdating. I believe this is because the context has already changed when the *ing-methods are called (already in some .svc).
    – eirikb
    Commented Sep 25, 2013 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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