Freigeben über


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:foobar.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

Comments

  • Anonymous
    May 22, 2007
    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

  • Anonymous
    May 22, 2007
    The comment has been removed

  • Anonymous
    June 06, 2007
    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.

  • Anonymous
    August 04, 2007
    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.Dictionary2.get_Item(TKey key) &nbsp; &nbsp; &nbsp;at Microsoft.Office.InfoPath.Internal.ReadOnlyDictionary2.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

  • Anonymous
    September 06, 2007
    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

  • Anonymous
    September 12, 2007
    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

  • Anonymous
    September 14, 2007
    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

  • Anonymous
    September 14, 2007
    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

  • Anonymous
    October 11, 2007
    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. :)

  • Anonymous
    October 11, 2007
    I should say VBScript.........

  • Anonymous
    December 27, 2007
    The comment has been removed

  • Anonymous
    January 20, 2008
    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

  • Anonymous
    January 21, 2008
    The comment has been removed

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

  • Anonymous
    January 23, 2008
    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?

  • Anonymous
    January 24, 2008
    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

  • Anonymous
    January 24, 2008
    Hi Scott i have commented on your "Submitting to 'this' document library" article. thanks again

  • Anonymous
    February 21, 2008
    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

  • Anonymous
    June 23, 2008
    I&#39;m currently working on a project that involves the modernization of lotus notes application to

  • Anonymous
    August 26, 2008
    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!

  • Anonymous
    October 10, 2008
    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

  • Anonymous
    June 04, 2009
    The comment has been removed

  • Anonymous
    December 20, 2009
    The comment has been removed

  • Anonymous
    August 17, 2010
    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 :(

  • Anonymous
    November 14, 2010
    The comment has been removed

  • Anonymous
    January 19, 2011
    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.

  • Anonymous
    February 07, 2011
    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.

  • Anonymous
    March 07, 2011
    Is this viable for SharePoint/InfoPath 2010 ?

  • Anonymous
    January 14, 2014
    Is this for infopath 2007 or 2010? I have no programming menu under my tools.

  • Anonymous
    March 11, 2015
    Any updates for this same question but with InfoPath 2013, SharePoint Designer 2013 in an Office 365 environment?