次の方法で共有


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

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(“https://microsoft.com”)

     }

    }

}

Comments

  • Anonymous
    March 24, 2008
    PingBack from http://msdnrss.thecoderblogs.com/2008/03/25/can-we-get-the-httpcontext-in-custom-event-handlers-in-sharepoint/

  • Anonymous
    March 24, 2008
    PingBack from http://msdnrss.thecoderblogs.com/2008/03/25/can-we-get-the-httpcontext-in-custom-event-handlers-in-sharepoint/

  • Anonymous
    March 26, 2008
    The comment has been removed

  • Anonymous
    April 01, 2008
    Exactly, since while adding an item to a list, I am redirecting the user to an another site, Thus the sample code will abort the current thread. My intention was to just provide a sample code on how we can get that httpcontext in event handlers. I saw your blog regarding this and it was really a great presentation. Thanks, Sowmyan

  • Anonymous
    June 30, 2010
    This dont work: properties.UserDisplayName.ToString(); ??

  • Anonymous
    June 06, 2011
    I am unable to find httpcontext object. its return null here below is mu code namespace RestrictFolderEventReceiver.FolderAccessDenied {    /// <summary>    /// List Item Events    /// </summary>    public class FolderAccessDenied : SPItemEventReceiver    {        HttpContext currentContext;        public FolderAccessDenied(): base()        {            currentContext = HttpContext.Current;        }          } }

  • Anonymous
    December 30, 2011
    I have same problem as Paresh Rao. why my HttpContext.Current in constructor return null?

  • Anonymous
    June 22, 2014
    I was able to get HTTPContext in custom event handlers. But i am not able to capture user input from Javascript Confirm pop up. is there a way round? Response.Writ() of HTTPContext to get pop up. I am getting pop up but not able to save user input.