Welcome to MSDN Blogs Sign in | Join | Help

InfoPath Team Blog

Tips and tricks to get the most out of Microsoft InfoPath

News

  • For questions, comments, and feedback please use the public newsgroup: microsoft.public.infopath
    This is provided "AS IS" with no warranties, and confers no rights. Use of included script samples and forms are subject to the terms specified in the Terms of Use.
Passing Data into a Form: Input Parameters

This blog article discusses a new feature of Microsoft InfoPath 2007 that makes it possible to pass data into an InfoPath form at load time. A typical example would be retrieving records from a database for a particular user. At load time a ‘userID’ can be passed into the form. This userID can then be used to query the database and load the form with the user's data.

Parameters can be passed into InfoPath form templates (XSNs) or InfoPath Forms (XMLs). The syntax for specifying input parameters is the same for both. This article focuses primarily on InfoPath client scenarios, but should apply for the most part to server scenarios as well.

How to Pass Parameters into an InfoPath Form:

There are two ways of launching an InfoPath form with parameters

1) URL

The syntax for passing parameters via the URL is the standard syntax for query parameters. For example:

http://www.foo.com/bar.xsn?baz=1&biz=2

Here two input parameters have been passed into the form namely 'baz' and 'biz'. Their respective values are 1 and 2. The 'Accessing Input Parameters in Form Code' section talks about how these values are stored and accessed in InfoPath code.

The URL syntax can be used in a number of places like

  • Launching an InfoPath form in a browser by typing the URL into the address bar
  • Pasting the URL to a form or a form template into an email
  • Using the URL inside methods like NewFromFormTemplate, New, Open (XmlForms collection)
2) Command Line

The syntax for passing parameters via the command line is as follows:

infopath.exe “C:\foo\bar.xml” /InputParameters "baz=1&biz=2"

The switch /InputParameters is used to specify that parameters are being passed into the form, followed by the name/value pairs of input parameters.

Accessing Input Parameters in Form Code

Parameters passed into an InfoPath form template or form, are available during the Loading event as a read-only collection of name/value pairs. In order to access these parameters you will need to write code that reads from this collection. The InputParameters collection is exposed in all three InfoPath programming models – it is thus available in JScript, InfoPath 2003 SP1 managed code or InfoPath 2007 managed code. This example uses C# and the InfoPath 2007 managed object model. Syntax for the legacy models follows. The steps below outline how to add code that access the Input Parameters passed into a form.

  1. In the InfoPath designer, click on Tools menu -> Programming menu item.
  2. In the fly-out menu select the Loading event (will be On Load in older InfoPath versions).
  3. This will launch the appropriate IDE (MSE or VSTA or the Visual Studio Toolkit) with the code spit for the loading event inserted.
  4. Add the following code to access the input parameters ‘baz and ‘biz used in the examples above (example is in C# using InfoPath 2007 Managed Object Model)

public void FormEvents_Loading(object sender, LoadingEventArgs e)

{

   // Assign the value of the parameter 'baz' to the string 'bazValue'. bazValue = 1

   string bazValue = e.InputParameters["baz"];

 

   // Assign the value of the parameter 'biz' to the string 'bizValue'. bi000zValue = 1

   string bizValue = e.InputParameters["biz"];

 

   // Code that uses the parameters passed in to do whatever needs to be done

   // Example would be to create a custom query using these values and populate a table based

   // on the data returned
}

Input Parameter Syntax for Legacy Object models

The following two code samples contain code for the InfoPath 2003 SP1 Managed Object Model (C#) and Jscript (InfoPath 2003 Object Model)

1) C# InfoPath 2003 SP1

In the InfoPath 2003 SP1 Object Model the InputParameters collection is exposed off the XDocument object not off the eventArgs object. Also since this is a new feature the XDocument object needs to be cast to the newer _XDocument3 interface to get access to the InputParameters collection. Hence the first line of code in the sample below.

 [InfoPathEventHandler(EventType = InfoPathEventType.OnLoad)]

 public void FormEvents_OnLoad(DocReturnEvent e)

 {
    
// Cast XDocument to _XDocument3 to get access to the InputParameters collection
    
_XDocument3 infopath2007XDocument = (_XDocument3)e.XDocument;

     string bazValue = infopath2007XDocument.InputParameters["baz"].Value;

     string bizValue = infopath2007XDocument.InputParameters["biz"].Value;

 }

2) JScript

function XDocument::OnLoad(eventObj)
{
      var bazValue = eventObj.XDocument.InputParameters["baz"].Value;
      eventObj.XDocument.UI.Alert(bazValue);
}

Help

Comprehensive documentation and additional code samples for this feature can be found under MSE Help or VSTA Help.

Aditi Desai
Software Design Engineer in Test

Posted: Monday, February 26, 2007 5:43 AM by infopath
Filed under:

Comments

ramkumar1910 said:

Great introduction!

it will be very helpful if you could write a bit more on how to use the nput param.

For exampe:

how to pass the input param(student id)  to the web service to get the details of one student and display in the form. Thanks

# May 22, 2007 3:27 PM

Scott Heim said:

Hi ramkumar1910,

Here is some sample code that I tested on the Loading event - note this does not have any error handling though:

string strCustID = e.InputParameters["CustID"];

           DataSource dsCustomers = this.DataSources["GetSelectedCustomer"];

           XmlNamespaceManager ns = this.NamespaceManager;

           XPathNavigator xnCustomers = dsCustomers.CreateNavigator();

           XPathNavigator xnCustID = xnCustomers.SelectSingleNode("/dfs:myFields/dfs:queryFields/tns:GetSelectedCustomer/tns:CustID", ns);

           xnCustID.SetValue(strCustID);

           dsCustomers.QueryConnection.Execute();

In this sample, I had a secondary data connection called GetSelectedCustomer that takes a CustomerID to pull information for just that customer. I then use the InputParameter to set the "queryField" value of my data connection and then execute that connection.

I hope this helps!

Scott

# May 22, 2007 3:57 PM

BobC said:

Is there a way to set a parameter at the content type or form library level?  I have tried adding a parm in the List Content Type Advanced settings like so:

/FormServerTemplates/MJWorkRequest2007.xsn?View=HelpDesk

but wss seems to lose the ability to open the form since .xsn is no longer at the end of the string.

# June 6, 2007 4:21 PM

swaven said:

Hi,

I have a question regarding connecting two infopaths.

How can we connect two infopaths through a common column?

I have an infopath with CustID1 and this infopath contains another file

attachment(infopath) and I have to popup the second infopath with the same

CustID1.

I tried the following code in the OnLoad event.

try

          {

              string strCustID = e.InputParameters["CustID"];

              DataSource dsCustomers = this.DataSources["GetCustID"];

              XmlNamespaceManager ns = this.NamespaceManager;

              XPathNavigator xnCustomer = dsCustomers.CreateNavigator();

              XPathNavigator xnCustomer = xnCustomer.SelectSingleNode("/dfs:myFields/dfs:queryFields/tns:Customers/tns:CustomerID");

              xnCustomer.SetValue(CustomerID);

              dsCustomer.QueryConnection.Execute();

          }

          catch (exception e)

          {

              messagebox.show(e.message,"Error");

          }

but encountered the following error:

System.Collections.Generic.KeyNotFoundException was unhandled by user code

Message="The given key was not present in the dictionary."

Source="mscorlib"

StackTrace:

     at System.ThrowHelper.ThrowKeyNotFoundException()

     at System.Collections.Generic.Dictionary`2.get_Item(TKey key)

     at Microsoft.Office.InfoPath.Internal.ReadOnlyDictionary`2.System.Collections.Generic.IDictionary<K,V>.get_Item(K key)

     at Customer_Checklist.FormCode.FormEvents_Loading(Object sender, LoadingEventArgs e)

     at Microsoft.Office.InfoPath.Internal.FormEventsHost.OnLoad(DocReturnEvent pEvent)

     at Microsoft.Office.Interop.InfoPath.SemiTrust._XDocumentEventSink2_SinkHelper.OnLoad(DocReturnEvent pEvent)

Is this the process for transferring the value of a field from one infopath to another infopath.  

Thanks in Advance

# August 4, 2007 11:24 PM

zullu said:

Hi Scott,

Your sample code was of great help to understand few more concepts in InfoPath 2003.

Thanks again for this.

I have another small question.

Can we create a secondary data connection with a filter criteria set as parameter to receive a value of a sharepoint list column for the current record pointer.

Thanks for all your help,

zullu

# September 6, 2007 10:49 AM

zullu said:

I was able to figure out my stuff through a web service for the second alternative way.

Thanks to responses from David Dean, Sr. Member Technical Staff, Insource Technology Corp. and Kit Kai's Blog post at the below url:

http://community.sgdotnet.org/blogs/kitkai/pages/Consuming-Web-Servic...

which helped me to resolve my issue quickly.

Thaks again.

zullu

# September 12, 2007 11:30 AM

shamrez said:

Hi

Thanks for this informative article. I have a question. If I pass parameters from one InfoPath form to another, I know that is possible but what If I want to send data back to the first InfoPath form. How will I do that? I am using web forms and my InfoPath template is hosted in a forms library in SharePoint. I want to open a new InfoPath form from my xsn template. After doing some processing in the newly opened form I would like to send the processed data back to the original form that is hosted in SharePoint. Can this be done? Plz help!

Thanks,

Sham

# September 15, 2007 3:57 AM

shamrez said:

Some more info! I created an aspx page that has a xmlformview control and this page is hosted in SharePoint (main site on port 80). I send some parameters in querystring to this aspx page from an InfoPath form hosted in SharePoint. I do some business processing and now I want to send the data back from this aspx page to the InfoPath form opened from SharePoint. How will we do this? Is it possible at all? Please help!

Thanks,

Sham

# September 15, 2007 4:02 AM

AD28 said:

Is there any way we could see the VB version of the InfoPath 2003 SP1 which is displayed in C# above?

I am very unfamiliar with syntax of both, but need to have this functionality written in VB however.

I would be so thankful for any help. :)

# October 11, 2007 10:32 AM

AD28 said:

I should say VBScript.........

# October 11, 2007 10:39 AM

CraigSchaeferWU said:

From the comments above, it appears that

 XDocument.InputParameters["x"].Value

should work with Infopath 2003.  However when I implemen this I get the following error:

 The form contains XML that cannot be parsed:

 An invalid character was found in text content.

 Line 1, Position 5

 MSCF

 ----^

I know the code is working becuase it works fine on Infopath 2007 clients.  Should this work for infopath 2003?

# December 27, 2007 2:01 PM

sakons said:

Hello

I use infopath 2007  inside sharepoint 2007 and I would like to pass parameters to the form via the querystring but it doesnot work the way you show it - no data enter the controls

I am posting the url

http://portal.tec.com/en/Zell/Application/_layouts/FormServer.aspx?year=2005XsnLocation=http://portal.tec.com/en/Zell/Application/ZellApplication/Forms/template.xsn&SaveLocation=http://portal.tec.com/en/Zell/Application/ZellApplication&Source=http://www.tec.com

It is a bit different from the your example  but its the url I can get out of the new item in the sharepoint list the form is in it

10x

Shlomy

# January 21, 2008 4:10 AM

sukebeta said:

I am using InfoPath 2003 SP3. I was trying to use JScript to get the input parameter from the URL. My url looks like this:

http://sharepoint/test/forms/form1.xml?view=Edit

Then on the OnLoad event of the form, I was trying to get the value of parameter "view":

var viewValue = eventObj.XDocument.InputParameters["view"].value;

eventObj.XDocument.UI.Alert(viewValue);

I got an error like this

A runtime error has occured. do you want to debug? the following error has occured:

'XDocument.InputParameters.view' is null or not an object

File: script.js

Line: 58

anyone know why it didn't work?

# January 21, 2008 11:18 AM

forrestd said:

As it says in the first paragraph, Input Parameters are a new feature for InfoPath 2007.

# January 21, 2008 9:11 PM

dkarantonis said:

hi to all,

i am using the e.InputParameter["List"] in order to get the list ID from the url and i get the exception "The given key was not present in the dictionary.". The List exists on the URL (this is a workflow list association form).

Any ideas?

# January 24, 2008 7:34 AM

infopath said:

Hi dkarantonis,

In this scenario, the "List" parameter is not an "InputParameter" to the form. If you debug this you will see the InputParameter count is zero. I have been unable to find a way to get this from behind the InfoPath form - so you may want to look at trying to get the URL and parsing it from the ASPX page.

Scott

# January 24, 2008 9:25 AM

dkarantonis said:

Hi Scott

i have commented on your "Submitting to 'this' document library" article.

thanks again

# January 25, 2008 3:35 AM

JohnCHill said:

Hello,

I understand the parameter piece of this but I cannot figure out how query or filter the data to only show records that match the parameter criteria. Can you help or point me to some other documentation?

Thanks!

John

# February 21, 2008 9:05 AM

Sundar Narasiman said:

I&#39;m currently working on a project that involves the modernization of lotus notes application to

# June 23, 2008 12:37 PM

mike11stevens said:

Has anyone successfully used input parameters in a SharePoint / Forms Server scenario?  Like several other commentors here, I am having trouble getting the url correct/working for my Forms Server forms...any/all help here would be greatly appreciated - thanks!

# August 26, 2008 1:38 PM

RRichie said:

I believe there is a bug in InfoPath 2003 SP3 as with several machines and several forms I get

The form contains XML that cannot be parsed:

An invalid character was found in text content.

Line 1, Position 5

MSCF

----^

Same code works in InfoPath 2007. As MainStreamSupport has just ended, I guess there is no hope for a bugfix?! However, my 450k-Employee-Org is still using InfoPath 2003...

# June 4, 2009 8:02 AM
Anonymous comments are disabled
Page view tracker