Programming InfoPath Web Forms

You have seen how to create a web enabled InfoPath form. You have also seen techniques to publish the form. What Next? How to program a web form! We will add code to the form that we created in one of the previous articles. We added to buttons. The buttons did not do anything. We will add the "Submit" functionality. Open the form in InfoPath editor.

1. Right click the "Submit" button and select "Button Properties".

Properties

2. In the "Action" drop down, select "Submit" and then click "Submit Options" button (It will appear after you select "Submit" in Action drop down).

3. Check "Allow users to submit this form" option. From the drop down that gets enabled, select "SharePoint document library".

properties

There will be an "Add" button underneath the drop down. Click it to add a data connection.

4. Add URL of the forms library in the "Document Library" box.

publish

By default, the name of the saved XML forms will be "Form". This will pose a problem because only form with this name will be saved. For new forms that you will try to save will give you an error because of the name conflict. The solution is to check the "Allow overwrite if file exists" option but will that solve the problem. Not necessarily! That will work if you want to save only one form in the library but for multiple forms, each form must have a unique name. Click the "fx" button. This will open the "Insert Formula" wizard.

5. Click "Inert Function" button.

wizarx

6. From the "Functions", select "concat" function and click OK.



7. Remove the first two parameters from the function and add "Form-" in place of them. Double-click the third parameter "double click to insert field" to insert a field. It will open the list of fields available in the form. Select "LastName". Your form will now be named as "Form-" plus the last name of the user taken from the form. For example, if the last name of the user is "Doe", the form name will be "Form-Doe". Click OK to save the settings. Click "Next" to move to the next step.

8. Enter a name for this data connection or leave the default name. Click "Finish" and then click OK. Click Ok again to close the wizard.

9. Right-click the second button and select "Button Properties". Click the "Rules" button. Click "Add" button. Click "Add Action" button. From the "Action", select "Close this form" and click OK. Click Ok thrice to close all the wizards.



10. Publish the form by selecting "Publish" from the "File" menu. Upgrade the form on the server. See previous article for help on this OR run the following commands from the command prompt:

stsadm.exe -o UpgradeFormTemplate -filename "C:\Forms\userregistrationtpl.xsn"

stsadm.exe -o execadmsvcjobs

Change file name and path in the "filename" parameter before running the commands.

Now, you have upgraded the form on the server. Open the form in the forms library and enter some dummy data and click the "Submit" button. The form will be submitted and you will see a javascript message.




Similarly, to test the close functionality, click the "Close" button. That will close the form.

Everything ok till now! Where is the fun part? The programming?

11. Right click the "Submit" button and select "Button Properties".  From the "Action" drop down, select "Rules and Custom Code". This will change the label. Change the label back to "Submit". Now, click the "Edit Form Code" button. This will open the Visual studio. Visual Studio Tools for Applications (VSTA) must be installed on your machine for this to work.

12. You will see default code.



This is the place where you can play around. You can add your code in the Submit_Clicked() function to do something when the "Submit" button is clicked. For example, you can add a default value in one of the fields programmatically. Add following code in the "Submit_Clicked()" function:

XPathNavigator nav = this.CreateNavigator().SelectSingleNode("/my:myFields/my:Country", this.NamespaceManager);

nav.SetValue("USA");


This will set the value of "Country" field to "USA" when the "Submit" button is clicked. Let's add some more code. Add following code to the "Submit_Clicked()". This will submit the form programmatically.

FileSubmitConnection SubmitConnection = (FileSubmitConnection)this.DataConnections["Main submit"];

SubmitConnection.FolderUrl = LibLocation.Substring(0, LibLocation.LastIndexOf("/"));

SubmitConnection.Execute();


Add FormEvents_Loading() function in the class.

public void FormEvents_Loading(object sender, LoadingEventArgs e)

{

}


Add following code in FormEvents_Loading():

//Edit mode

if (e.InputParameters.ContainsKey("XmlLocation"))

{

Location = e.InputParameters["Source"].ToString();

Location = Location.Substring(0, Location.IndexOf("/", 8));

LibLocation = Location + e.InputParameters["XmlLocation"].ToString();

}

else

{

//New mode

LibLocation = e.InputParameters["SaveLocation"].ToString();

}


Add following event handler in "InternalStartup()" function:

EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);


Finally, add the global variables to hold the form session data. These variables are used in the "Submit_Clicked()":

 private string LibLocation
{
get
{
return (string)FormState["LibLocation"];
}
set
{
FormState["LibLocation"] = value;
}
}

private string Location
{
get
{
return (string)FormState["Location"];
}
set
{
FormState["Location"] = value;
}
}

The complete code will look like this:

using Microsoft.Office.InfoPath;

using System;

using System.Xml;

using System.Xml.XPath;

namespace userregistration

{

public partial class FormCode

{

// Member variables are not supported in browser-enabled forms.

// Instead, write and read these values from the FormState

// dictionary using code such as the following:

//

// private object _memberVariable

// {

// get

// {

// return FormState["_memberVariable"];

// }

// set

// {

// FormState["_memberVariable"] = value;

// }

// }

// NOTE: The following procedure is required by Microsoft Office InfoPath.

// It can be modified using Microsoft Office InfoPath.

public void InternalStartup()

{

EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);

((ButtonEvent)EventManager.ControlEvents["Submit"]).Clicked += new ClickedEventHandler(Submit_Clicked);

}

private string LibLocation

{

get

{

return (string)FormState["LibLocation"];

}

set

{

FormState["LibLocation"] = value;

}

}

private string Location

{

get

{

return (string)FormState["Location"];

}

set

{

FormState["Location"] = value;

}

}

public void FormEvents_Loading(object sender, LoadingEventArgs e)

{

//Edit mode

if (e.InputParameters.ContainsKey("XmlLocation"))

{

Location = e.InputParameters["Source"].ToString();

Location = Location.Substring(0, Location.IndexOf("/", 8));

LibLocation = Location + e.InputParameters["XmlLocation"].ToString();

}

else

{

//New mode

LibLocation = e.InputParameters["SaveLocation"].ToString();

}

}

public void Submit_Clicked(object sender, ClickedEventArgs e)

{

XPathNavigator nav = this.CreateNavigator().SelectSingleNode("/my:myFields/my:Country", this.NamespaceManager);

nav.SetValue("USA");

FileSubmitConnection SubmitConnection = (FileSubmitConnection)this.DataConnections["Main submit"];

SubmitConnection.FolderUrl = LibLocation.Substring(0, LibLocation.LastIndexOf("/"));

SubmitConnection.Execute();

}

}

}


13. Compile the code (Build > Build <form name>). Re-publish the form. Go to forms library and open the form. Add First and Last name. Click the "Submit" button. You will notice the "Country" field will be populated with "USA". The form has been saved in the library. Close the form and you will see a link in the library.

Hope you enjoyed the article.

Download application