Wednesday, July 23, 2008

Controlling the SharePoint Redirection URL

For anybody who has ever tried to customize SharePoint, you may have encountered the following problem:

You create a page which has a link to a list item's EditForm.aspx, NewForm.aspx, or DispForm.aspx. However, if a user clicks one of these links, then subsequently closes that page by pushing Cancel or OK, they are redirected back to the corresponding list or library, rather than to the page they were previously on (i.e. your custom page).

SharePoint uses a querystring argument to control the return url on any given page. By default, this parameter is set to the respective list or library for the current form.

You can control the return url by modifying or adding this parameter to any link.

Example:

<a href="/Lists/MyList/NewForm.aspx?Source=/Pages/MyCustomPage.aspx">Click Me!</a>

When the link is clicked the user will be directed to the new form for "MyList". After clicking OK or Cancel, they will be redirected back to "MyCustomPage.aspx".

Simple enough, right? Fine, let's dig a little deeper.

This works great if you are creating your own link through a Content Editor WebPart or SharePoint designer, but what about if you are using one of the List web parts, and want to control the redirection for the 'Add new item' link, or some other content you can't modify directly.

This is where we get to hack out a solution in JavaScript.

To put it simply, you can create a custom JavaScript function to append the "Source" querystring argument to the desired links.

For example, put the following into a text file, and save it as AddSourceUrlToLinks.js:

**************************************************************************

//Tell SharePoint to run our function when the page loads
_spBodyOnLoadFunctionNames.push("appendSourceUrlToLinks");

//The function that appends the Source url to links
function appendSourceUrlToLinks()
{
//Collect all links on the page
var links = document.getElementsByTagName("a");

//Loop through the links
for (var curLink=0; curLink <>0
links[curLink].href.indexOf("EditForm.aspx")>0
links[curLink].href.indexOf("DispForm.aspx")>0)
{

//optional, ignore links that already have a source url
if(links[curLink].href.indexOf("Source=") <=0) { if(links[curLink].href.indexOf("?")>0)
links[curLink].href = links[curLink].href + "&Source=" + escape(window.location);

else
links[curLink].href = links[curLink].href + "?Source=" + escape(window.location);

}
}
}
}

**************************************************************************

Save this file in the following location on the SharePoint server:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extenstions\12\Template\Layouts\1033

The final step is to add a javascript reference to the custom page via SharePoint Designer (unfortunately, I don't think there is any other way...).

Find the following asp tag in the code of your custom page:

<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">

Add the following JavaScript reference directly below it:

<script type="text/javascript" src="/_layouts/1033/custom_webpart_script.js"></script>

Save your page, and giv'r a go.

That's it! Obviousely you can customize the code to your liking. Play around with it. I have also used a similar method to append other querystring arguments to pages that use a querystring filter.

No comments: