Simple Redirection Web Part

This is so easy and has so many applications within SharePoint. I’ve lost count of all the times SharePoint users have complained about being thrown at the base of a list after they close an item. Most of the time the users shouldn’t even know about the list itself- they have just accessed a news item link on a home page or something similar. So here is a solution to redirect users away from a list base that they have access to but don’t need to see:

Edit the list page and add a Content Editor web part. Edit the source and add this script:

 <script type=”text/javascript” language=”javascript” for=”window” event=”onload”>

if (window.location.search.length < 1)
window.location.href = “http://www.google.com“;

 </script>

Click Ok. Now you will notice that the page does not redirect straight away. This is because the a query string (window.location.search) is present : ?PageView=Shared.  If you navigate away from the list and then navigate back to it you will be redirected to Google.  If you want some users to have access to the base of the list you can just provide them with the link containing a query string e.g. http://server/lists/listname/allitems.aspx?noredirect. When the web part works to your liking, you can Export the web part (Edit-> Export) and reuse it on other pages and sites.

If you end up accidentally causing a constant redirect when playing around with your script logic, just copy the list url into the address bar and then add ?contents=1 to the end and you will be able to delete the CE web part using the Web Parts maintenance page.

Update: this is a bit nicer and looks for a specific query string: ?redirect=false to prevent redirection

<script for=”window” event=”onload”>

if (window.location.search.substring(1) != “redirect=false”)

window.location.href = http://www.google.com;

 </script>

Advertisements

2 thoughts on “Simple Redirection Web Part

  1. What will happen when you try to access the list from the site content page? Those links don’t contain a querystring so you will always get redirected.

    –W

    • Yes, thats the point. The web part would redirect in all cases where there is no query string. If you wanted to access the list, you’d add any old query string value to the end of the URL. This particular example would just be used for keeping users away from the base list (for what ever reason). In this case the admin may want to make the list available somewhere else e.g. via a list web part. The provided script is just an example. The post is actually trying to highlight the concept of using the content editor web part as a rediection tool.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s