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 asp.net page and hosting that page within Sharepoint using the XmlFormView class.

This technical guide explains the process

http://msdn.microsoft.com/en-us/aa701078.aspx

The code behind for the asp.net 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", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-07-08T12:49:29");
// 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
fTextBoxPosition.SetValue(positionAppliedFor);

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

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

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.

Thanks.