In this article, we will create a web-enabled InfoPath form that will have managed code in its "on load" that will populate a field automatically. We will deploy our InfoPath form to a SharePoint form library.
First off: this article assumes you are using MOSS 2007, Microsoft Office InfoPath 2007, and Visual Studio 2008. To add managed code to an InfoPath form, Visual Studio Tools for Applications (VSTA) must be installed.
If VSTA is not set up, the following message appears:
"InfoPath cannot add the event handler.
To work with Visual Basic or C# code, VSTA is required. .NET Framework 2.0 and Microsoft Core XML Services 6.0 must be installed before VSTA."
To install VSTA:
----------------
1. Control Panel -> Add or Remove Programs -> Microsoft Office 2007 -> Change
2. Under Microsoft Office InfoPath -> .NET Programmability Support, check the .NET Programmability Support for .NET Framework version 2.0 and then under that check Visual Studio Tools for Applications
Also, be sure Microsoft Core XML Services 6.0 is installed (it will show up as MSXML6). Check for it in Add or Remove Programs, and if it is not installed, search for it on microsoft.com, then download it.
Now, on to why we are here...
To create a web-enabled InfoPath form with managed code:
--------------------------------------------------------
1. Create the form in InfoPath. I won't go in detail here. Just create a form with a few fields. Call one of the fields AccountId.
2. change compatibility settings to make it web-enabled:
Tools -> Form Options -> Compatibility -> check Design a form template that can be opened in a browser or InfoPath
3. give the form Full Trust:
Tools -> Form Options -> Security and Trust -> select Full Trust
4. add code - to have custom code when the form loads:
a. set location where you want the Visual Studio project to be created
Tools -> Programming -> under Programming Language, specify language and project location
b. indicate you want to code for the loading of the form
Tools -> Programming -> Loading Event... (it will prompt you to save the form
if you haven't already)
VSTA will create a Visual Studio project (using the form's name) in the location
specified in a. Visual Studio will load with project opened.
c. add code to populate a form field automatically when form loads
under FormEvents_Loading, add code to execute when form loads
Dim strAccountId As String = ""
strAccountId = "P-" & Date.Now.ToString()
'populate account field on infopath form with account #
Me.CreateNavigator.SelectSingleNode("my:MyForm/my:AccountId", Me.NamespaceManager).SetValue(strAccountId )
5. run the project to see the form field being populated
To deploy a web-enabled InfoPath form with managed code:
--------------------------------------------------------
6. publish the form template
back in InfoPath,
a. click Publish Form Template...
b. select To a network location and click Next
c. enter a location to publish the template, a place where an administrator can access and click Next, clear textbox then click Next, click Publish, click Close
7. upload Form Template in Central Admin
a. in Central Administration -> Application Management -> Manage Form Templates, click Upload Form Template
b. browse to location you just published to and select form template
c. click Upload
8. activate the form at the site collection level
In Site Settings -> Site Collection Features, find form template and click Activate
Notice the form template appears as a Site Content Type under Site Settings -> Site Content Types
9. create and set up a form library and add that content type to it
create new form library
a. I go in through View All Site Content -> Create -> Form Library
b. give it a Name
c. Document Template should have Microsoft Office InfoPath form
d. click Create
10. set up library
a. settings -> form library settings -> advanced settings
check Yes for Allow management of content types
check Display as a web page
check No for Display "New Folder" command on the New menu (I just like to remove NewFolder, this isn't really necessary)
click Ok
b. Under Content Types, remove Form and add new content type (form template)
a. click Form -> Delete this content type
b. click Add from existing site content types, select new content type, click Add, then click OK
11. go back to form library and click New
12. You should now see the form display as a web page, and notice the AccountId field was populated programmatically!