InfoPath: Transfer Data Between 2 Forms - SharePoint Development & Administration + InfoPath

Sunday, August 30, 2009

InfoPath: Transfer Data Between 2 Forms

Overview:
When added to an InfoPath Form template, the following JScript allows data to be transferred from one form to another programmatically using code executed by the first form. For example, this can allow a set of data from a forms data sources to be extracted and transfered into another form containing similar fields. This can also allow variations, or custom versions of a form template to be sent to specific people who may speak a different language, require an alternate layout or theme, or if the data containined in the form is confidential and needs to be submitted to a secure or different location to other forms of the same type.

Alternative Methods for Transferring data between forms (without using code)


Requirements:
- InfoPath 2007 (can also be acheived using 2003)

Process:
The step descriptions below have been commented out to allow them to be pasted into the forms code. Additional notes are in blue and have also been commented, but can be ommited if required.
There are a few ways you can get the code to run. The easiest would probably be an event handler function associated with a button on the form, as the function is generated by InfoPath when you select "Edit form code" from the "Button Properties" dialog. Other options are to add the code to the submit function, which is executed by InfoPath when the form is submitted. If using "Rules and Custom Code", a combination of rules and the example below can be used to submit both form #1 and #2.





/* -----------------------------------------------------
JScript: Transfer data between two forms
22nd Feb 2009
-------------------------------------------------------- */



//1. Set the path to the "form #2" template:

var strNewFormPath = "http://path-to-infopath-form-template";


/*
Note: If the path to the form template is a UNC path ("file:///\\filesrv06\users\john\Forms\Form2.xsn"), the backslash "\" is a special character which is used to escape other special characters, including the backslash it'self. The address above will actually be interpreted as "file:///\filesrv06usersuserFormsorm2.xsn", or similar which will result in the following error: "The following URL, path, or file name is invalid".

To ensure that all characters in the address string are interpreted correctly, the backslash "\" character needs to be escaped for it to remain in the string (see below):
"file:///\\\\filesrv06\\users\\john\\Forms\\Form2.xsn" will be interpreted as "file:///\\filesrv06\users\john\Forms\Form2.xsn" when executed.
*/




//2. Open the second form:
var objNewForm = Application.XDocuments.NewFromSolution(strNewFormPath);
/*
Note: This will result in InfoPath opening the second form from the address supplied in step 1. If you recieve a runtime error at this line in the code, check to make sure that special characters have been escaped, and that the location of the form template is accessable to all users of the form.
*/




//3. Set the name spaces for the new form:

objNewForm.DOM.setProperty("SelectionNamespaces", 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-01-29T23:32:25" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003"');

/*
Note: To find the name space for a data source, go to the properties of a field in the data source, then the details tab. The name space in the details tab should be all that you need.
You will need to use the name spaces from your data sources, instead of the examples above (and in step 5).
*/




//4. Load the XML Document of "form 2":
var objExternalData = new ActiveXObject("MSXML2.DomDocument.5.0");objExternalData.validateOnParse = false;objExternalData.loadXML( objNewForm.DOM.xml );
/*
Note: This loads the XML Document of the second form's main datasource, assigning the DOM Document to a variable which is then used in step 6 to access the second form's data source from the first form.
*/




//5. Set the name space for the XML data object:

objExternalData.setProperty("SelectionNamespaces", 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-01-29T23:32:25" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:ns1="http://schemas.microsoft.com/sharepoint/soap/directory/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"');




//6. Update the value of fields in the main or secondary data source of "form #2" with values from "form#1":

objNewForm.DOM.selectSingleNode("/my:myFields/my:Name").text = XDocument.DOM.selectSingleNode("/my:myFields/my:Name").text;
objNewForm.DOM.selectSingleNode("/my:myFields/my:Description").text = XDocument.DOM.selectSingleNode("/my:myFields/my:Description").text;

/*
Note: The above two lines will copy the values from the Name and Description fields in Form 1 to the Name and Description fields in Form 2.
The "objNewForm.DOM.selectSingleNode..." selects the required field in the secon form, which is then assigned the value from the corresponding field in the first form: "XDocument.DOM.selectSingleNode..."
*/


Alternative Methods for transferring data between forms without using code:

If using SharePoint you can submit the data to a SharePoint list from the first form eith using a CAML query and the SharePoint lists web service or the forms main submit data source, then load the data into the second form from the SharePoint list using a secondary data connection. If not using SharePoint, this can also be achieved by submitted to an external database (Access) or even an xml file (default) which can then be used as a secondary datasource in the second form to retrieve the data.

Share this article:
Stumble This Delicious
Delicious
submit to reddit
Facebook
MySpace
MySpace

No comments:

Post a Comment