Is it allowed to access SPWeb and specifically SPWeb.ID from PreRequestHandlerExecute ?

Writing an HttpModule [SharePoint2010] and trying to access current web ID using this line of code :

private void context_PreRequestHandlerExecute(object sender, EventArgs e)
Guid  curID = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(System.Web.HttpContext.Current).ID;

All seems fine but when using the designer, an error is generated :

"soap:ServerServer was unable to process request. ---> Cannot complete this action.     Please try again. ---> Cannot complete this action. Please try again."

EDIT : Note that SPContext works fine in PreRequestHandler, it exists and all fields returh the correct values. During QA, we found out that accessing Spweb.ID causes the Desginer to throw an error .. this is disturbing since there might be implications of using SPContext which are not documented at all.

3 Answers 3


You can try to use somwthing like this

    public void Init(HttpApplication application) {
        application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
        application.EndRequest += (new EventHandler(this.Application_EndRequest));

    // Your BeginRequest event handler.
    private void Application_BeginRequest(Object source, EventArgs e) {
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        SPContext spctx = SPContext.GetContext(context);
  • 1
    I tried your solution but SPContext is not accessible during BeginRequest, only during PreRequestHandlerExecute is can be used. When used in PreRequestHandlerExecute it exists and works but it seems there are implications that might cause severe bugs .. these implications are not documented anywhere Jul 25, 2012 at 12:47

Remember that your HTTP Module effectively runs outside SharePoint. It won't have things like an SPContext yet.

I would try using the requested URL to get the current web:

string url = "http://sp/somesite/lists/somelist/etc";
using(SPSite site = new SPSite (url) {
  using(SPWeb currentWeb = site.OpenWeb(){
    //CurrentWeb should contain the site that the request refer to.
  • As I understand SPContext can be used in HttpModule (for example this post talks about it ). Also note, the code works and Web.ID retunrs the correct ID, the exception is thrown further down the line .. Jul 25, 2012 at 12:08
  • I stand corrected. So you're saying that you get this error in SharePoint Designer only?
    – Andy Burns
    Jul 25, 2012 at 14:05
  • Yes. During testing we noticed that accessing the ID field in SPWeb has unexplained implications - errors in the designer. It worries me that there might be more use cases which we didn't check .. Jul 25, 2012 at 15:20

I've had the same issue. I also used the PreRequestHandlerExecute event of the HTTPModule, because I needed SPContext (which is not available yet in BeginRequest).

The reason why SPD wasn't working for my situation was that SPD did a request to '_vti_bin/WebPartPages.asmx' which results in a 'Internal Server Error' when my custom HTTPModule was handling the request. Unknown why, because I was only handling particular request and ignoring other onces.

But I managed to fix it with the code below. When a request comes in with a '.asmx' extension or 'owssvr.dll' (which was also causing problems with my custom HTTPModule, when changing List Settings), the request is just ignored with simply calling 'return';

 void context_PreRequestHandlerExecute(object sender, EventArgs e)
     var context = HttpContext.Current;
     string absolutePath = context.Request.Url.AbsolutePath.ToLower();
     if (absolutePath.Contains("owssvr.dll") ||

     //Your custom code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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