Best way to Link to Forms on Web Pages - Shiraz Cupala
in

InfoPath Dev

Shiraz Cupala

Best way to Link to Forms on Web Pages

Whenever you want to expose your forms on a web page the tendency is to want to put a plain hyperlink to your XSN form template or XML file. The problem with this ranges from getting lame IE download dialogs all the time to data loss scenarios (see below). The correct way is to do it the way SharePoint does it.

You want to use script to call the inlaunch.dll which InfoPath installs and registers just for this purpose.

Key Scenarios:

  1. Open an existing XML form file for editing. - Here you want to use the EditDocument2() method. If you don't then in some cases users can experience dataloss as outlined here: http://www.infopathdev.com/forums/t/6045.aspx
  2. Create a new form from an XSN form template. - For this you call the CreateNewDocument2() method.
  3. Create a new form using an existing XML file as the default data. - For this you use an undocumented feature whereby you pass the URL to your XML file to the same CreateNewDocument2() method. Essentially what it does is open that XML file but forces a SaveAs type behavior.

 

Brief Code Snippet

This example is for the second scenario: 

var strTemplateLocation_New = "http://sitename/folder/template.xsn";
var strDefaultSaveLocation_New = "
http://www.example.com";
function CreateNewXMLDocument()
{
    // Based on WPQ3combineDocuments()
    var objOpenXMLDoc = new ActiveXObject("SharePoint.OpenXMLDocuments.1");
    var bSuccess = false;
    bSuccess = objOpenXMLDoc.CreateNewDocument2(window , strTemplateLocation_New , strDefaultSaveLocation_New );
}


<a onclick="CreateNewXMLDocument(); return false;" style="cursor: pointer;" href="<numbersign>">Create New Blank Form</a>

Note: where you see href="<numbersign>" you need to put in the # symbol inside the quotes instead.  I can't show that here because the blog posting system reformats it as code. Best is to just download the sample code directly.

Full Sample Code

I've uploaded a web page with sample code for all 3 scenarios. Download Example: http://www.infopathdev.com/files/folders/examples/entry41177.aspx

Some notes on this example:

  • You will need to swap in your XSN or XML urls for the current placeholders for "DocumentLocation" and "TemplateLocation".
  • The use of 'http://www.example.com' is to just force InfoPath to instead ask the user to save into their MyDocuments folder. You can alternatively specify a hard drive location but wildcards won't work. Also both these methods will result in a browser cross-domain security prompt. To eliminate that you would want to use the same server name as in the actual XSN link. If that is a SharePoint site this will result in InfoPath thinking it can save to that location (which may be true). If it's not a SharePoint site then you're in luck because then InfoPath will treat it like the example.com placeholder and force the SaveAs to MyDocuments but without any cross-domain issues/prompt.
  • You'll notice the hyperlinks have hrefs and the script. This is so that if the user wants to right-click download the target they can do this (for example to save an XML file for offline use). I of course substituted # for the XSN link as downloading the XSN locally makes no sense.
  • LIMITATION: If you use this method to link to a file not on the same domain then you will get a cross-domain security yes/no dialog from Internet Explorer and maybe other browsers. It is because script is trying to access a file on another domain. I currently know of no way around this issue.

 

MSDN OM Documentation

The following MSDN reference is the complete documentation of this method and explains a couple other advanced calls for other less common scenarios: http://msdn2.microsoft.com/en-us/library/ms452435.aspx

 

Post Update on 2007-10-22 to explain further an issue around the example.com placeholder.
Post Update on 2007-10-23 to correct the ProgID in the sample htm document to use "InfoPath.Document" instead of "mso-application".

 

Comments

 

lonuel said:

Can you use this method to work around the need to install a form that is fully trusted to access a database?

February 5, 2008 1:14 PM
 

Twitter Trackbacks for Best way to Link to Forms on Web Pages - Shiraz Cupala [infopathdev.com] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Best way to Link to Forms on Web Pages - Shiraz Cupala         [infopathdev.com]        on Topsy.com

April 20, 2010 2:55 PM

About Shiraz Cupala

Shiraz worked for 10 years at Microsoft in product design, web development, customer research, and online marketing roles. Over the first 3 versions of Microsoft InfoPath he managed Outlook, Excel and SharePoint integration, the hosted InfoPath control, IW and developer programmability features, product usability, and internal solutions. He has architected numerous small and enterprise level InfoPath solutions including for collaboration, publshing, and finance, including the InfoPath solution for Microsoft's expense reporting system. Shiraz holds a Bachelor of Arts in Philosophy from Tufts University.
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.