Using the SPContext in HttpModules in SharePoint

When you are building custom HttpModules in SharePoint a common thing to do is to use the SPContext object to get acces to your SPSite, SPWeb, SPList or SPListItem. When doing this you have to be careful because the SPContext isn’t available everywhere because HttpModules can run early in the ASP.Net request pipeline so the SPContext object isn’t available yet.

if you try to acces the SPContext to early in the pipeline you’ll get a System.InvalidOperationException error.

The earliest you can use the SPContext is in the “PreRequestHandlerExecute” so don’t use it at the BeginRequest event what most people try to use when they want to add code as early in the pipeline as possible


   1: public class RequestHandlingHttpModule : IHttpModule, IRequiresSessionState

   2: {


   4:     public void Dispose() {}


   6:     public void Init(HttpApplication application)

   7:     {

   8:         application.BeginRequest += application_BeginRequest;

   9:         application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);

  10:     }


  12:     void application_PreRequestHandlerExecute(object sender, EventArgs e)

  13:     {

  14:         string webUrl = SPContext.Current.Web.Url;

  15:     }


  17:     void application_BeginRequest(object sender, EventArgs e)

  18:     {

  19:         // do not use SPContext here it will throw a System.InvalidOperationException 

  20:         // this event is fired before PreRequestHandlerExecute so code that doesn't uses SPContext goes here

  21:     }


  23: }

Geert van der Cruijsen

Share on Facebook
Kick It on
Shout it
Post on Twitter

Combined power of SharePoint and JQuery part 2: Changing the SharePoint webpart edit menu position

Have you ever had the problem using SharePoint 2007 that when you create small webpart zones that the edit button disappeared because the title of the webpart was to long? I ran into this problem at my current project and instead of shortening down the titles i came up with the idea of changing the position of the edit button to be on the left of the title. when the button is on the left and the title is on the right the title will be trimmed automatically since the webpart zones have a fixed length.

In your server side code it’s really hard to change these kinds of things because this is standard SharePoint functionality and you can’t change it server side. So the solution is to fix it using javascript and to make that easier I’m using JQuery.

first I’ll show you the results below and then how I did it using a small JQuery script.





Below is the JQuery script to change the position of the edit menu to the left. the only thing you have to do is save this code to a js file, include the reference to the js file and a reference to the JQuery library js file on your masterpage and you’re done.

   1: $(document).ready(function() {

   2:     SPEditMenuFix();

   3: });


   5: function SPEditMenuFix() {

   6:     $(".ms-WPHeader").each(function() {

   7:         var first = $(this).children("td:first").clone(true);

   8:         var last = $(this).children("td:last").clone(true);

   9:         $(this).children("td:first").replaceWith(last);

  10:         $(this).children("td:last").replaceWith(first);

  11:     });

  12: }


Geert van der Cruijsen

Share on Facebook
Kick It on
Shout it
Post on Twitter

Updated my weblog to DotNetBlogengine 1.6.1

Just before my vacation to Peru i noticed that i received a lot of spam in my comments on my blog. i was running DotNetBlogEngine 1.4.5 and the spamfilter seemed to be broken. I didn’t have time to fix it before my vacation so i disabled comments for a while. now that I’m back I upgraded to 1.6.1 which has build in functionality for Askismet and Recaptcha.

because I had so many spam comments I had to remove all of them so sorry for everyone who posted real comments because they are also gone now.

BlogEngine.Net 1.6.1 (download from codeplex) feels like its a lot more mature then the 1.4.5 version I had before. Lets see how well it will go in the future.

Because of the switch I had to change the theme because the old one didn’t work that well with this new version so i downloaded a new theme from and changed a bit to my liking. When i have time i’ll change it a bit more.

If you are still using an old version of BlogEngine.Net I would advise you to upgrade to 1.6.1 because it’s looking pretty good and upgrading was easy. (costed me 2 hours including deleting of spam Smile )

Enjoy my new weblog

Geert van der Cruijsen

Share on Facebook
Kick It on
Shout it
Post on Twitter