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 8 and type the answer here:
  • Post
  • 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

  • Hi Scott

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

    thanks again

  • 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

  • I'm currently working on a project that involves the modernization of lotus notes application to

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

  • u How to do custom themes for MOSS http://www.sharepointblogs.com/tigirry/archive/2007/07/03/custom-site-theme-for-sharepoint-2007-moss-2007-and-wss-3-0.aspx

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

  • So my question is, if I have data in a dropdown. How can i create a scheme where if in form "A" 6 items are selected from a dropdown box, then form submitted to library. Next user of the form "A", those 6 items are not availible in the drop down list. IE they are checked out? If sharepoint and infopath are these great collaboration tools, why can't I make them work in a dynamic role, Why must everything be so static?

    Please advise?

  • Hello all ,

    Is there any way to dynamicaly add those input parameters to form template instead of manually adding those query parameters , i have scenerio of 10 subsites and each subsite has 10 form libraries and i published template as content type to top site and iam trying to pass their respective subsite name and form library name as parameters to template url by changing the advanced settings of respective form library. so iam doing this this process 100 times :(

  • Hi!

    Thanks for this article!

    I have a problem with the URL. Setting the parameters with the Command Line everything is OK, but in URL only the forms opens without data.

    I'm using a webservice.

    I would be so thankful for any help!

  • Hello. What do you think about adding new feature (in next version):

    Include new Data Connection Type "From Parameters" for data loading from input parameters.

  • We have got this to work almost flawlesslessly with InfoPath 2007

    But we bumped into two problems.

    A) The input parameters used are persistent, because the entire comandline is stored in the mso-infopathsolution PI (Processing Instruction) section of the xml instance that is created.

    B) Opening up two instances of a form using input parameters concurrently may cause an error to occur with the following error message:

    [InfoPath cannot open the form because another version of the form template is currently open. Close all forms using the form template and try again.]

    We found that KB Article ID: 957385 support.microsoft.com/.../957385 adresses both these issues.

  • Is this viable for SharePoint/InfoPath 2010 ?

  • Is this for infopath 2007 or 2010?

    I have no programming menu under my tools.

Page 2 of 2 (29 items) 12