Thursday, 7 August 2008

Using query string with InfoPath forms

Sometimes it is necessary to accept query string for browser enabled info path forms. This can be done by hosting the Infopath form in a page and hosting that page within Sharepoint using the XmlFormView class.

This technical guide explains the process

The code behind for the page will contain a method like the one shown below..

protected void XmlFormView1_Initialize(object sender, InitializeEventArgs e)
string positionAppliedFor = Request.QueryString["Position"];
string jobID = Request.QueryString["JobID"];
string appURL = Request.QueryString["AppURL"];

if (!string.IsNullOrEmpty(positionAppliedFor) && (!string.IsNullOrEmpty(jobID)))
// Create an XPathNavigator positioned at the root of
// the form's main data source.
XPathNavigator xNavMain = XmlFormView1.XmlForm.MainDataSource.CreateNavigator();
// Create an XmlNamespaceManager and add the "my" namespace
// alias from the form's main data source.
XmlNamespaceManager xNameSpace = new XmlNamespaceManager(new NameTable());
xNameSpace.AddNamespace("my", "");
// Create an XPathNavigator positioned on the form's field2.
XPathNavigator fTextBoxPosition = xNavMain.SelectSingleNode(
"/my:myFields/my:gpRecommendation/my:txtPositionAppliedFor", xNameSpace);
// Set the form's job Position Value to the value passed in the query strings

XPathNavigator fTextBoxJobID = xNavMain.SelectSingleNode(
"/my:myFields/my:gpRecommendation/my:txtJobID", xNameSpace);

if (!string.IsNullOrEmpty(appURL))
XPathNavigator fTextBoxAppURL = xNavMain.SelectSingleNode(
"/my:myFields/my:gpRecommendation/my:txtAppURL", xNameSpace);

1 comment:

Raghavendra K said...

thanks for the post dude.. i had an exact requirement ..where i had to prefill the form using a querystring values. i was not supposed to use codebehind and this solution helped.