2

How can I get the SPWebApplication for the current request from Init method in a HttpModule?

3 Answers 3

3

Is SPContext.Current not yet available at this time? You should be able to get it from this. (SPContext.Current.Site.WebApplication from the top of my head)

Be careful though as this will call into the SharePoint object model for each incoming request, which will most likely hit the database as well. This may wreak havoc on performance and scalability.

1
  • The Init method is called only once per thread in the application pool. SPContext is null at this stage. In my code Init method calls another method OnStartup that does some initialization and does this only once per AppDomain. Mar 28, 2011 at 20:00
1

What about

SPSite site = new SPSite(YOUR_SITE_COLLECTION_URL);
var webApp = site.WebApplication;

I didn't have a reason to use this, so I don't know if it works, it's just an ideea. Try it, hope it helps

1
  • This can work but will hardcode the site collection URL. Is not an option even to read it from web.config file. Apr 1, 2011 at 18:14
1
 public void Init(HttpApplication application)
    {
        //This is the first event that has SPContext populated for use
        application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);

    }

Then in the application_PreRequestHandlerExceute you can access SPContext.Current.

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.