• Home
  • @Spotd – Realtime stuff
  • about
  • chicago dive bars
  • tamale guy tracker
  • the current playlist widget
Blue Orange Green Pink Purple

URL rewriting with SharePoint (MOSS 2007)

Posted in code, internet, sharepoint, web. on Thursday, August 14th, 2008 by djskinnyfat Tags: asp.net, c#, httpmodule, moss 2007, moss2k7, sharepoint, url rewriting, wss
Aug 14

Whats up? Url rewriting is a pretty cool thing to have. [especially when people ask you for it] There’s a lot of requests by people who want to implement full on url rewriting or extension less urls in SharePoint 2007. After messing around for a little bit it, I came up with a good way to do it. When I was looking for examples on the web there were a lot of people saying that it couldnt be done. I thought, wtf, serial? But in the end it actually didn’t take much to implement it. Here’s an HttpModule that I wrote to do the work.

The key pieces are the this.app.BeginRequest += new EventHandler(app_BeginRequest) which
steps in front of the request and allows the module to get its redirect on.

And HttpContext.Current.RewritePath(redirect, false); will push the necessary headers n such forward so that the receiving .aspx page will understand how to correctly post back.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Web.SessionState;
using System.Security.Cryptography;
using System.Configuration;
using System.Threading;
using System.IO;
using System.Security;
using System.Security.Principal;

namespace ScaredPanda
{
    public sealed class RewriteHttpModule : IHttpModule
    {
        HttpApplication app = null;
        ///
        /// Initializes the httpmodule
        ///
        public void Init(HttpApplication httpapp)
        {
            this.app = httpapp;
            this.app.BeginRequest += new EventHandler(app_BeginRequest);
        }

        public void app_BeginRequest(Object s, EventArgs e)
        {
            try
            {
		//determine if the income request is a url that we wish to rewrite.
		//in this case we are looking for an extension-less request
                string url = HttpContext.Current.Request.RawUrl.Trim();
                if (url != string.Empty
                    && url != "/"
                    && !url.EndsWith("/pages")
                    && !url.Contains(".aspx")
                    && url.IndexOf("/", 1) == -1)
                {
                    //this will build out the the new url that the user is redirected
                    //to ie pandas.aspx?pandaID=123
                    string redirect = ReturnRedirectUrl(url.Replace("/", ""));

		    //if you do a HttpContext.Current.RewritePath without the 'false' parameter,
                    //the receiving sharepoint page will not handle post backs correctly
		    //this is extremely useful in situations where users/admins will be doing a
                   //'site actions'  event
                   HttpContext.Current.RewritePath(redirect, false);
                }
            }
            catch (Exception ex)
            {
                //rubbish
            }
        }
    }
}

Comments are closed.

scrrd panda

  • recently written
    • @Spotd – Realtime stuff
    • Delete all comments from Wordpress
    • 45/365 Clear Crisp City
    • Tamale Guy Tracker
    • Slowly Fade Into The Darkness
  • Flickr Photostream

    Oops, there seems to be a problem accessing the Flickr photos.

  • categories
    • amsterdam
    • arsenal
    • chicago
    • code
    • excursions
    • internet
    • life
    • music
    • sharepoint
    • sport
    • trance
    • Uncategorized
    • web
  • Archives
    • April 2009 (2)
    • February 2009 (1)
    • January 2009 (6)
    • October 2008 (2)
    • September 2008 (1)
    • August 2008 (3)
    • July 2008 (2)
    • June 2008 (3)
    • May 2008 (4)
    • April 2008 (2)
  • Archives
    • April 2009
    • February 2009
    • January 2009
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
  • Search






  • Home
  • @Spotd – Realtime stuff
  • about
  • chicago dive bars
  • tamale guy tracker
  • the current playlist widget

© Copyright scrrd panda. All rights reserved.
Designed by FTL Wordpress Themes brought to you by Smashing Magazine

Back to Top