Microsoft InfoPath 2010
The official blog of the Microsoft InfoPath team

Passing Data into a Form: Input Parameters

Passing Data into a Form: Input Parameters

Rate This
  • Comments 29

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

Leave a Comment
  • Please add 3 and 3 and type the answer here:
  • Post
  • 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

  • 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

  • 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.

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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. :)

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

  • 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?

  • 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

  • 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?

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

  • 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?

Page 1 of 2 (29 items) 12