With the InfoPath Forms Services capabilities in Microsoft Office SharePoint Server 2007, you can develop Microsoft Office InfoPath 2007 solutions that extend beyond the corporate firewall, delivering forms for Web browsers or even mobile devices. In browser scenarios, your forms can contain most of the rich functionality that is available in InfoPath 2007, and the publishing process for these forms is very easy.
Whether or not you use managed code in your form template, the publishing process starts with the InfoPath Publishing Wizard. For this scenario, you use the travel request sample that is included with Office InfoPath 2007, add a few lines of managed code to calculate a trip duration, and then publish the form template to InfoPath Forms Services.
Adding a Duration Node
Start the travel request sample template in the InfoPath designer, and follow these steps to add a Duration node.
To add a Duration node to the form view
In the Design Tasks task pane, click Data Source.
In the Data Source task pane, expand the trips node.
Right-click the trips node, and then click Add.
In the Add Field or Group dialog box, in the Name box, type Duration, and then click OK.
Next, update the form view to include an expression box that is bound to the Duration node.
In the form view, in the TRIP section, place the cursor to the right of the text Round trip and press SPACE three times.
Drag the Duration node from the Data Source task pane to the right of the space characters you just added to the form view.
Right-click the Duration text box, click Change To, and then click Expression Box.
Reduce the size of the Duration expression box so that it appears on the same line as the corresponding heading, as shown in Figure 1.
Figure 1. Adding Duration to the form view
You must save the form template locally before you add any code.
Including a Calculation Private Function
To add managed code to your form solutions, you can use Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office system (VSTO 2005 SE) or Microsoft Visual Studio 2005 Tools for Applications (VSTA). Office InfoPath 2007 includes Visual Studio 2005 Tools for Applications, which you use in this scenario to add a private function that calculates the total number of days in a given period.
You can set the programming language to Visual Basic or Visual C# before you add managed code to your form template. To do this, on the Tools menu, click Form Options, click the Programming category, and set the Form template code language list value accordingly.
To include a calculation function
On the Tools menu, click Programming, and then click Microsoft Visual Studio Tools for Applications.
In the FormCode class, add the following code.
private string GetDuration(string startDate, string endDate)
long longStartDate = DateTime.Parse(startDate).ToFileTimeUtc();
long longEndDate = DateTime.Parse(endDate).ToFileTimeUtc();
long longDayCount = (longEndDate - longStartDate) / 864000000000 + 1;
retValue = longDayCount.ToString();
In the GetDuration function, the longDayCount long type identifies the difference in ticks between two date values divided by 864000000000 (the constant number of ticks in a day) plus 1. The result is the duration in days for the two date values and becomes the return value for the function.
Adding the Changed Events
The Duration expression box is meant to be updated with a new calculated value each time the departure date or return date is changed. Therefore, you add a Changed event for the Departure Date and Return Date date picker controls.
To add Changed events
Right-click the Departure Date date picker control, click Programming, and then click Changed Event.
In VSTA, in the departureDate_Changed procedure, add the following code.
string departDate = e.Site.InnerXml;
string returnDate = e.Site.SelectSingleNode("../my:roundTrip/my:returnDate", NamespaceManager).InnerXml;
if (departDate != string.Empty && returnDate != string.Empty)
e.Site.SelectSingleNode("../my:Duration", NamespaceManager).SetValue(GetDuration(departDate, returnDate));
Next, you return to the InfoPath designer and add a Changed event for the Return Date date picker control.
Right-click the Return Date date picker control, click Programming, and then click Changed Event.
In VSTA, in the returnDate_Changed procedure, add the following code, which is slightly different from the code you added to the departureDate_Changed procedure.
string departDate = e.Site.SelectSingleNode("../../my:departureDate", NamespaceManager).InnerXml;
string returnDate = e.Site.InnerXml;
if (departDate != string.Empty && returnDate != string.Empty)
e.Site.SelectSingleNode("../../my:Duration", NamespaceManager).SetValue(GetDuration(departDate, returnDate));
Each Changed event makes a call to the GetDuration procedure and sets the Duration node accordingly.
Updating Postback Options
Before the form template is deployed, you must update the two date picker controls to ensure that data is sent to the server each time the departure date or return date is changed.
To update the date picker controls
Close VSTA, and in InfoPath, double-click the Departure Date date picker control.
In the Date Picker Properties dialog box, click the Browser forms tab.
Click Always, and then click OK.
Repeat the previous three steps for the Return Date date picker control.
Press CTRL+S to save the form template.
Deploying the Form Template
Because the form template uses managed code for a browser scenario, it must be admin-deployed. In other words, you must first publish the form template to a network share.
If a form template does not contain managed code, you can publish it directly to a SharePoint site.
To publish a form template to a network share
On the File menu, click Publish.
On the first page of the Publishing Wizard, click To a SharePoint server with or without InfoPath Forms Services, and then click Next.
On the next page, in the Enter the location of your SharePoint or InfoPath Forms Services site box, type the URL for the SharePoint site, and then click Next.
On the next page, note that the Administrator-approved form template option is selected, and click Next.
On the next page, click Browse, and browse to a network share location.
In the File name box, type TravelRequest, click Save, and then click Next.
On the next page, note the promoted columns, and click Next.
On the next page, click Publish.
On the last page of the Publishing Wizard, click Close.
After the form template is published to a network share, a server administrator can upload that form template to InfoPath Forms Services and activate it to a specific Windows SharePoint Services 3.0 site collection.
To upload a form template to InfoPath Forms Services
Click Start, click All Programs, click Microsoft Office Server, and then click SharePoint 3.0 Central Administration.
On the Central Administration page, click Application Management.
In the InfoPath Forms Services section, click Manage form templates.
On the Manage Form Templates page, click Upload form template.
Click Browse, locate TravelRequest.xsn, and double-click it.
Click Upload, and then click OK.
Wait until the Status value for TravelRequest.xsn changes from Installing to Ready.
On the TravelRequest.xsn menu, click Activate to a Site Collection.
Update the Site Collection list value accordingly, and then click OK.
Starting the Travel Request Form
To test your changes, start the travel request form template in the browser and modify the Departure Date and Return Date fields.
To modify the Departure Date and Return Date fields
In Microsoft Internet Explorer, navigate to the browser location of your form template, which will be http://Your_Web_Application_URL/_layouts/formserver.aspx?xsnlocation=/formservertemplates/travelrequest.xsn&openin=browser.
The travel request template has a rule, executed when the form starts, that sets the Departure Date to today and the Return Date to three days from today.
Select the Round trip check box.
Change the Return Date to tomorrow.
Change the Departure Date to yesterday.
InfoPath 2007 enables you to develop form templates that can run in InfoPath Forms Services, thus allowing them to be accessed from a Web browser. With Visual Studio Tools for Applications, you can use the InfoPath event framework to easily add managed code to these form templates. To admin-deploy these form templates:
Use the InfoPath Publishing Wizard to save the form template to a network share.
Have a server administrator upload that form template to InfoPath Forms Services and then activate it to a specific site collection.
Watch the Video
Video Length: 00:09:33
File Size: 9.08 MB WMV