Can we get the HTTPContext in custom event handlers in SharePoint ?

Can we get the HTTPContext in custom event handlers in SharePoint ?

  • Comments 8

Yes, but not everywhere. Means, we will get the HTTPContext from the initial call of the base class default constructor from the particular class which inheriting the event receiver class. I have tested only with SPItemEventReceiver class and it does work and I hope it will work fine for all other event receivers.

I saw lots of articles and forums says, we can’t get the HTTPContext in an event handler, then I had researched some more deeper, then found that we will get it, but we can use it only in synchronous events like ItemAdding, ItemUpdating etc, we won’t get it in asynchronous events like ItemAdded , ItemUpdated etc.

using System;

using System.Collections.Generic;

using System.Text;

using System.Web;

using Microsoft.SharePoint;

 

namespace TestEvent

{

    public class MyCustomEventHandler : SPItemEventReceiver

    {

        HttpContext hContext = null;

 

        public MyCustomEventHandler() : base()

        {

            hContext = HttpContext.Current; // we will get

                 the HttpContext from the default constructor , but we have

                 to provide the

 

        }

 

      public override void ItemAdding(SPItemEventProperties properties)

        {

           hContext.Response.Redirect(“http://microsoft.com”)

        }         

 

    }

}

Comments
Page 1 of 1 (8 items)
Leave a Comment
  • Please add 4 and 3 and type the answer here:
  • Post