Tags
« WindowsPhone 7.5 Mango is so GOOD | Main | 10 InfoPath tips for SharePoint developers »
Friday
Jun172011

InfoPath 2010 close the browser window

How do you close the browser window (or tab) completely when you close an InfoPath browser form?

Quick steps:

  1. In your URL to open your form, specify the source parameter.  This is the URL InfoPath will return the user to after the browser form is closed.
  2. Create a simple SharePoint web page with a bit of JavaScript to close itself.
  3. Set the source parameter to this page.

 

1. Use the Source

MSDN documentation: http://msdn.microsoft.com/en-us/library/ms772417.aspx

When you close a browser form, either via the Close ribbon button, or via the Close action in rules, when the source parameter isn't specified, you end up on this page:

image

Figure: Silly and really redundant.

If you specify a source parameter, when the user closes the form the browser will redirect to that URL.  This could be the homepage of your site.

With Form Server you can use a few special values such as ~sitecollection or ~site

 

2. Create a web page that closes itself.

Create a page (wikipage or publishing page), and add this JavaScript magic here.

<script language="javascript" type="text/javascript">
    window.open("", "_self");
    window.close();
</script>

 

Why is this javascript so strange?  The reason is that if you just call window.close() directly, most browsers will check if the current window was opened from a parent window (such as a popup), and will throw an offensive prompt to the user.

image

Figure: JavaScript close window confirmation - we don't want this

 

There are a few different ways to trick the browser, the one that I use above basically tells the browser window to open itself, so now the browser thinks it's got a parent (which is itself), and now won't prompt the user for close confirmation.

 

3. Set the URL parameter

 

http://server/_layouts/FormServer.aspx?XmlLocation=/ProjectForms/227.xml&Source=~sitecollection/Pages/close.aspx&DefaultItemOpen=1

So now when you close your browser form, it will redirect to our close.aspx page, which will actually close the web browser window (or tab).

Reader Comments (29)

Awesome.. thanks.. you saved my day

January 26, 2012 | Unregistered CommenterPaul

Great tip! Appreciate you sharing.

I was able to use this on a Sharepoint List New Item link I am opening in a new browser window. I wanted the window to close after clicking Ok to save the new item vs going back to the List itself upon completing. Now we are able to feed the List data without uncessary page clicks/views to the List itself afterwards

May 30, 2012 | Unregistered CommenterAnonymous

thanks heaps. worked like a charm!

August 23, 2012 | Unregistered CommenterKelly Anderson

Could you provide a sample of the the code with the source - I cannot get this to work.
Using a custom web part page with a SharePoint list. this does not work. Form window stays open

apparantly I can't put code in here as I was going to show you my string

September 12, 2012 | Unregistered CommenterAmy

Hi Amy,

When your InfoPath form closes, is it redirecting to the page that you've created?

If it is, then check your close page - your javascript might be incorrect, so it's not self-closing.

September 24, 2012 | Registered CommenterJohnLiu.NET

Excellent!!

Even with my very limited javascripting knowledge i managed to pull this off.

I created an .aspx page with only the javascript mentioned above. Within my sitecollection i uploaded this .aspx page as a "Site content type". Next up was using the URL of this site content type as the source location of my web form: http://ictformulieren.d1.zg/_cts/Close/close.aspx

Now, on closing or submitting the form it perfectly closes the browser window or tab.

Can't believe i got this to work... :-D

Thanks a million!!

October 10, 2012 | Unregistered CommenterSjaaks

Awesome! Thank you for the tip as well.

October 29, 2012 | Registered CommenterJohnLiu.NET

Brilliant - THANKS!

February 5, 2013 | Unregistered CommenterPoolio

In my case, It's not working...

function showAddFeedbackDialog()
{
var options = {
title: "Test",
allowMaximize: false,
autoSize:false,
width: 1200,
height: 750,
url: "http://server/site/_layouts/FormServer.aspx?XsnLocation=~sitecollection/FormServerTemplates/Form.xsn&SaveLocation=~sitecollection/Library/Forms&Source=~sitecollection/SitePages/Close.aspx&OpenIn=Browser"
};
SP.UI.ModalDialog.showModalDialog(options);

But it redirects me always to "The form has been closed"

April 27, 2013 | Unregistered CommenterMario

Hi, my process is built using K2 blackpearl. I integrate InfoPath to SharePoint and I want the same thing as you are talking above. I want to redirect to a sharepoint calendar list. I do not know where to set the url parameter.
Kindly show me the place where I can go to set the url parameter for the source.
Thank in advance.

May 21, 2013 | Unregistered CommenterWandath

Hi Wandath,

If you set the Source parameter to your calendar list that should work.

May 21, 2013 | Registered CommenterJohnLiu.NET

Hi Mario,

I find that with new forms (XsnLocation), you don't need the Source parameter. The XSN closes by itself. I've only used the Source=~close.aspx trick when I'm using XmlLocation parameter for existing forms.

May 21, 2013 | Registered CommenterJohnLiu.NET

Hi JohnLiu.NET,

The thing is that I do not know where to set the Source parameter. On InfoPath? Where? I really have no idea.
Thanks.

May 21, 2013 | Unregistered CommenterWandath

Hi Wandath,

maybe the following isn't helpful at all, but still i'll let you know:

By design, users are expected to open InfoPath forms from the designated forms library. I, on the other hand, let my users open the form from another location by simply using the URL of the forms (each form has its own URL). In this URL i have set the source, here's an example of a URL of one of my forms:

http://ictformulieren.d1.zg/_layouts/FormServer.aspx?XsnLocation=http://ictformulieren.d1.zg/FormServerTemplates/RechtenWijzigen3.xsn&SaveLocation=http%3A%2F%2Fictformulieren%2Ed1%2Ezg%2FICT%20Formulieren&Source=http%3A%2F%2Fictformulieren%2Ed1%2Ezg%2FDefault%2Easpx&DefaultItemOpen=1

Notice the "Source" part. Here it says http://ictformulieren.d1.zg/Default.aspx&DefaultItemOpen=1.
Normally it would be: http://ictformulieren.d1.zg/ICTFormulieren/Forms/MijnFormulieren.aspx.

By letting my users open the forms on another page and therefore using the URL's, i can manually set the Source so after closing or submitting the form, they will automatically be sent to the Source of my choice instead of being sent back to the library. This way you can redirect your users to http://www.google.com or whatever...

Hopefully this will help you in any way possible.

Kind regards...

May 21, 2013 | Unregistered CommenterSjaaks

Hi there,

Thank you for the answer. Actually, the problem was that I was trying to use XmlLocation parameters and not XsnLocation, which is the sort of form I have.
Commonly mistake, but you're right, once the form is closed it redirect where to the page determined by "&source=".

How modify this? Well, go to your library, create a new item and once your Infopath form is opened, in the browser bar, you'll see the path of this form. You'l be able to see "&source=" and the path which will be opened once you close your form. I hope this information works.

May 21, 2013 | Unregistered CommenterMario

Hi Mario,

Would you please share with me how to set the parameter of the "&source=" ? I have a form library which users can add new item by filling in the Infopath form that is opened. After user click on submit, I want to redirect user to a calendar list on the same site.
From what I read above, I understand that we need to set the path of Calendar list to the "&source=". My question is that how can I set the path to that "&source=" so that every time the form is opened, "&source=" will contain the path there.
Thank in advance.

May 22, 2013 | Unregistered CommenterWandath

Hi,
I understand now. I don't need to add new item from Add document icon on its webpart itself. I created a link to open the InfoPath form. Inside that link I specified the path of the source attribute.
Thanks everyone.

May 22, 2013 | Unregistered CommenterWandath

IS this working with a form in an InfoPath Web Part ? I can't make it work when the form is displayed in a Web Part. Any ideas ?

August 17, 2013 | Unregistered CommenterMarco

I'm thinking with the InfoPathWebPart, you can try using two pages.

/Pages/_close.aspx
/Pages/_redirectToClose.aspx


//add this script
//cause the top level window that's hosting the current iframe to redirect to the close page.
window.top.location = "/Pages/_close.aspx";

August 30, 2013 | Registered CommenterJohnLiu.NET

thank you
Great post
I added java script by content web part and test the close.aspx it is close when I request then I added the source parameter to link
than kyou twice

October 2, 2013 | Unregistered CommenterMohammad Hawasli

I was having issues trying to figure out how to run the script. The following website was very helpful to me along with your info.

http://www.kalmstrom.com/Tips/JavaScriptInSharePoint.htm

December 27, 2013 | Unregistered CommenterTimK

Thank you! just what I needed

February 10, 2014 | Unregistered CommenterGuy Perry

Hi..Thank you so much..

March 2, 2014 | Unregistered CommenterSatya

Could you help in this scenario: My initial form URL includes an &Source which redirects the user to another Pages/_confirmation.aspx page with a form web part. This form web part has URL links to redirect the user to two different aspx pages depending on the process flow - one to confirm/close and redirect the user to Home.aspx or a second hyperlink to an aspx page to continue with another form - both URL hyperlinks work fine but I can't get this current browser window to close. I've tried to embed the close.aspx page in the hyperlink as &Source. I have the same issue to close the third .aspx browser page in the final submit step. This particular Form Web Part needs to be set to "open new form". I have tested the close page which closes automatically so I know the javascript is working. I don't understand where to use this additional script when using two pages: //add this script
//cause the top level window that's hosting the current iframe to redirect to the close page.
window.top.location = "/Pages/_close.aspx";

I am not a developer so a solution without coding or using javascript in a CEWP would be preferred. Thank you for your consideration.

April 26, 2014 | Unregistered CommenterPatrice

> a solution without coding or using javascript in a CEWP would be preferred

This is pretty tough. I am a SharePoint Dev. :-O

InfoPath forms really aren't designed for the scenario where you can chain different forms and control the navigation, but we can try.

In the case where you have a single Form and on close, you want to go to a new form:

Try go to a temporary redirect page. And from the redirect page, send the user to the new form with a new &source= param defining where to go after that.


In the case where you have a single Form with multiple hyperlinks:

In each hyperlink in the form, include the &source= param controlling what to do after that form is closed.

May 19, 2014 | Registered CommenterJohnLiu.NET

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>