0

I'm trying to use SPUtility.Redirect in ItemAdding EventReceiver. And I'm trying to get HttpContext.Current in Event Receiver constructor, but it's null

    public class MyClass : SPItemEventReceiver
    {

       HttpContext currentContext;

        public MyClass()
        {
               currentContext = HttpContext.Current; // is null
        }

        public override void ItemAdding(SPItemEventProperties properties)
        {
               HttpContext currentContext = HttpContext.Current;
               SPUtility.Redirect("http://microsoft.com", SPRedirectFlags.Default, currentContext);
        }
        //.....
    }

Any ideas? SharePoint version is 2013.

0

This is as designed. It's possible that eventreceivers do not have access to current context.

/* edit*/ There are some changes in SharePoint 2013. See this article: http://blog.sharepointalist.com/2013/03/sharepoint-2013-event-receiver-redirect.html

In SharePoint 2010:

Implement one of the eventreceiver methods in the class you created. In your case override the 'ItemAdding' method and add your code in this method. Use the 'properties' object to determine the item, list and web the event applies to.

If you encounter an error set the following attributes of 'properties': Cancel, ErrorMessage, Status and RedirectUrl. See code below

    public override void ItemAdding(SPItemEventProperties properties)
    {

    base.ItemAdding(properties); 
    // your code here

    properties.Cancel = true;    
    properties.ErrorMessage = "An error occured...";
    properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
    properties.RedirectUrl = "/_layouts/YourFeatureName/CustomErrorPage.aspx";


    }
  • As far as I know synchronous EventReceivers could redirect user back in SP2010 version. Is this feature depricated in 2013? I also get [depricated] error on 'Cancel' and 'RedirectUrl' lines in your code sample – Chetvergov Dec 3 '14 at 8:11
  • you are right. Seems that they changed it in SP2013. You can only redirect when the adding is done by a form. Found this article blog.sharepointalist.com/2013/03/… – Wout Dec 3 '14 at 8:56

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.