Session and QueryString in Event Handler

Posted: October 7, 2008 in EventHandlers, HTTPContext, Object Model, QueryString, Session

Add System.Web namespace

public class MyEventHandler: SPItemEventReceiver
{
HttpContext current;

String requestNumber;
public MyEventHandler()
{
current = HttpContext.Current;
requestNumber=Convert.ToString(this.current.Request.QueryString[“reqno”]);
}
public override void ItemAdded(SPItemEventProperties properties)
{
// code  for event handler goes here

}

Since event handler methods doesn’t have access to the page object we cant access query string and Session values.
So you can use the current object to access the query string and Session values.

Advertisements
Comments
  1. Sillu says:

    HttpContext.Current will be null in this case.

  2. Chanakya says:

    No it will not be null.. It will be having the current request. This works fine with me..

  3. Sillu says:

    Suppose you're right. Maybe I did something wrong.

  4. Chanakya says:

    Make sure you initialize the HttpContext in the constructor..

  5. Sillu says:

    Yep, that was the case. But you can use the context only in synchronous events (i.e. ItemAdding, ItemUpdating).

  6. Chanakya says:

    I havent tried that.. But tat may be true because for the events like ItemAdded etc we are handling them once the event is completed so the response will be ended once the event is executed.

    But still we are initializing the object in the constructor and not the method.. So i think it will work..

  7. Suresh Subramaniam says:

    HttpContext.current is Null in ItemAdded when the item is added from event receiver & workflow. Is there any other work around for this?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s