Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question

3 Answers 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.

share|improve this answer
    
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. –  Emilian BALANESCU Mar 28 '11 at 20:00

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

share|improve this answer
    
This can work but will hardcode the site collection URL. Is not an option even to read it from web.config file. –  Emilian BALANESCU Apr 1 '11 at 18:14
 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.