Wednesday, June 06, 2007

Allowing InfoPath forms to redirect to pages in other site collections

In InfoPath 2007, when you publish a form as a web form, you have the option to specify several query string parameters. One of those parameters is the "Source" parameter, which is "The location to which the user will be redirected when the form is closed. The URL must be in the same site collection or an error will be returned."
The error mentioned is "The following location is not accessible, because it is in a different site collection:" and the url you specified.

So it seems that specifying a source from another site collection is impossible. But what if I have a site collection dedicated to forms? lets say "http://forms", to which I link from the intranet home page or another page, and I want that when the user closes the form, he will be redirected back to the page in the intranet (lets say "http://intranet") that we started from?

I had to come up with a solution, and a simple one jumped to mind - a redirect page!
The trick is simple. Instead of specifying a link to "http://intranet" as the source parameter (which will cause an error), specify a link to a special page in the layouts folder, and give that page the query string that links to the page you want to return to.

Confused? here is an example:
Instead of linking to the form with this link:

http://forms/_layouts/FormServer.aspx?XsnLocation=http://forms/myforms/Forms/template.xsn&Source=http://intranet

Link to it like this:
http://forms/_layouts/FormServer.aspx?XsnLocation=http://forms/myforms/Forms/template.xsn&
Source=http://forms/_layouts/SPSTipsFormRedirector.aspx?target=http://intranet

As you can see from the links, the first one will cause an error because we are linking to another site collection, while the second one will work, because we are linking to the current site collection.

So the only thing left for me to show you is how to create the SPSTipsFormRedirector.aspx page that will handle the redirect to the page you specified in the red section in the second link:
Open your layouts folder (C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS) and create a new text file by the name SPSTipsFormRedirector.aspx. Open the file in a text editor such as notepad, and copy the following code:

<%
string target = Page.Request.QueryString["target"];
Page.Response.Redirect(target);
%>
<%
@ Page Language="C#" Inherits="System.Web.UI.Page" %>
<html>
</
html>

Repeate this in every front end server, and you are ready to write links to forms that, when closed, will go back to a page you specify in the link!

6 comments:

itaysk said...

Thx for the tip.
If you dont want to write code and your redirect page doesn't have to be dynamic, you can use sharepoint's redirect page (under the create page there's a redirect page layout)

Rob said...

Should you not url encode the URL you are redirecting to as well, as it has a 2nd ? character to use the query string.

biilmann said...

Hi Ishai, thanks for the tip. Have you any ideas if I need to redirect to a dynamic URL? E.g. when my form is used to create a new site, that I then finally would like redirect to?

TIA, Thomas

RP said...

Alternative approach:

Use custom page similar to FormServer.aspx and handle XmlFormView.Close event.

Once the form is closed, you can redirect or perform any additional operations.

Leminwinks said...

This doesn't work for me. I just get an error. I have the source point to the custom page with a target but it does not re-direct to this target and just throws up a generic error. If I run the custom page in a browser by itself, it works fine.

Dylan said...

This was exactly what I needed. You're a lifesaver.
Thanks alot, keep up the good work!