DotNet Mirror
  DNM facebook   DNM Google+   DNM Twitter   

sharepoint spcontext is null in custom httpmodule

By Ashok Nalam on 25 Feb 2013 | Category: Sharepoint | Tagged: error 
  Discuss Add Comment   |  
Add rating Rate this resource   
When we try to access SPContext object in HttpApplication.BeginRequest event in custom httpmodule for SharePoint site, we will get an error like SPContext is null.

In order to overcome this issue, move your code to PreRequestHandlerExecute event.

Sample Code:
public class MyCustmHTTPModule : IHttpModule
{
    public void Init(HttpApplication appObj)
    {
        appObj.BeginRequest += new EventHandler(Application_BeginRequest);
        appObj.PreRequestHandlerExecute += new EventHandler(Application_PreRequestHandlerExecute);
    }

    public void Application_BeginRequest(object sender, EventArgs e)
    {
        //SPContext is null here
    }

    public void Application_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        //SPContext can be accessed here
        SPUser currentUser = SPContext.Current.Web.CurrentUser;
    }
}

  Discuss Add Comment    
Add rating Rate this resource   
About the Contributor
Member Since : 10 Dec 2012
Member Points (Level) : 8666  (Professional)
Location : INDIA
Home Page : http://dotnetmirror.com
About : I am admin of this site.
Rate this resource
 
Add your Comment
Name Email WebSite
Captcha Refresh


Comments (0)
No comments found, click here to add comment.