Tuesday, March 16, 2010

How to Pass / access Parameters into an InfoPath Form

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

In the InfoPath designer, click on Tools menu -> Programming menu item.
In the fly-out menu select the Loading event (will be On Load in older InfoPath versions).
This will launch the appropriate IDE (MSE or VSTA or the Visual Studio Toolkit) with the code spit for the loading event inserted.
Add the following code to access the input parameters ‘ID and ‘Status' used in the examples above

(example is in C# using InfoPath 2007 Managed Object Model)
public void FormEvents_Loading(object sender, LoadingEventArgs e)
{

try
{

if (HttpContext.Current != null)
{
if (HttpContext.Current.Request.QueryString.Count > 0)
{
string strTempId = HttpContext.Current.Request.QueryString["ID"];
CreateNavigator().SelectSingleNode("/my:myFields/my:RequestTitle", NamespaceManager).SetValue (strTempId);
}
}
}
catch (Exception ex)
{
//Write exception code

}
}

No comments: