Using Custom HttpHandler to redirect users in Sharepoint

13,366

Solution 1

HttpHandlers are used by IIS to "handle" different document types, for instance you have separate .asmx handle, .aspx handler, .ascx handler, etc.

Look into SPUtility.Redirect

You can use the SPUtility.Redirect method whenever you want to direct the user to a different page. For example, you might create a landing page that determines the user's role membership, and based on that information you can redirect them to an appropriate page. Or, based on the contents of a query string issued by the user's browser, you might redirect them to a page that can process the query string, such as the Search Center results page.

Alternatively, you can look into Using Disposable Windows SharePoint Services Objects.

--EDIT--

This is in response to your comment; you are thinking in the right direction

  1. Create a custom HttpHandler; Example 1, Example 2
  2. Whenever a user requests a url, your custom HttpHandler is going to process that.
  3. Redirect() if you like the url, else otherwise.

But for your Custom HttpHandler to work, it should be called first - so in your config file you will have to provide the value in path. Usually an extension is added here. But you can replace that with a * to work for all request. I believe that would work.

<httpHandlers>
    <add verb="*" path="*.aspx" type="MyNameSpace.MyHttpHandler, MyAssemblyName" />
</httpHandlers>

--EDIT--

This is in response to your comment. Assuming that you have "access" to pages so that you can write javascript in it, you can use the javascript in following way.

<script language=JavaScript>
function RedirectIfUnAuthorized()
{
    //Get the location of the current page.
    var currentUrl = new String( document.location.href ) 

    //See if it belongs to the urls you are looking for; redirect if so.
    if (currentUrl == "http://www.thisUrl.com/page1.aspx") {Response.Redirect("http://www.GOOGLE.com")}
    if (currentUrl == "http://www.thisUrl.com/page2.aspx") {Response.Redirect("http://www.BING.com")}
    if (currentUrl == "http://www.thisUrl.com/page3.aspx") {Response.Redirect("http://www.someother.com")}
}
</script>

You may call the above javascript in the page's OnLoad event.

Solution 2

Are you allowed to deploy code on the server? Or is that touching IIS too?

(SharePoint makes changes to web.config too. If you're allowed to deploy code, so could you. If you don't tell your admins they probably wouldnt even notice.)

You can 'deploy' your web.config changes through an SPWebConfigModification and deploy any web.config redirections or httphandlers that way.

Share:
13,366
BeraCim
Author by

BeraCim

SharePoint, .NET, jQuery, Javascript nub...

Updated on June 04, 2022

Comments

  • BeraCim
    BeraCim almost 2 years

    I need to redirect a user to a different page if they visit a certain set of pages in a web. There is one condition: I'm not allowed to touch the IIS. I have googled around and found a thing called the custom HttpHandler for WSS 3.0. It sounded like something that I can use to capture the URL the user enters (the most malicious way to get to a page that should really be redirected) and redirect them to another page/web. But having not have the chance to use it yet, I was wondering am I on the right track by using a Custom HttpHandler to redirect a user to a different page in Sharepoint using C#?

    Many thanks.