Calculating date differences in InfoPath using SharePoint Excel Services
Use an Excel workbook published to Excel Services on a Microsoft Office SharePoint Server 2007 to calculate the difference between two dates in InfoPath without writing a single line of code.
Problem
You have an InfoPath form with two date fields. You would like to calculate the difference between the two dates without writing code.
Solution
Create an Excel workbook to calculate the difference between two dates, publish this Excel workbook to Excel Services on a Microsoft Office SharePoint Server 2007, and call Excel Services' web services from within InfoPath to do the date difference calculation without writing a single line of code.
Discussion
You can accomplish this functionality as follows:
- Create a new Excel workbook.
- Rename cell A1 to StartDate and change its format to Date.
- Rename cell B1 to EndDate and change its format to Date.
- Rename cell C1 to DateDiff.
- Set the formula of cell C1 to =EndDate-StartDate.
- Rename sheet Sheet1 to CalcDateDiff.
Figure 1. Excel workbook to calculate date differences. - Publish the Excel workbook to a Trusted File Location for Excel Services.
- Design an InfoPath form as shown in figure 2 with two Date Picker controls
named startDate and endDate, one Text Box
control named dateDiff, and a Button control.
Figure 2. The InfoPath form template in design mode. - Open the Data Source pane.
- Add a Field (element) called sheetName of type Text (string) and set its Default Value to CalcDateDiff.
- Add a Field (element) called rangeNameStartDate of type Text (string) and set its Default Value to StartDate.
- Add a Field (element) called rangeNameEndDate of type Text (string) and set its Default Value to EndDate.
- Add a Field (element) called rangeNameDateDiff of type Text (string) and set its Default Value to DateDiff.
- The Main data source of your form should now resemble the following figure:
Figure 3. The structure of the Main data source of the form template. - Go to Tools > Data Connections....
- Click on Add... to open the Data Connection Wizard.
- Select the Receive data option and click on the Next > button.
- Select the Web service option and click on the Next > button.
- Type in the URL to Excel Services running on your server.
Example: http://<your_server_name>/_vti_bin/ExcelService.asmx?wsdl - Click on the Next > button.
- Select the OpenWorkbook web service and click on the Next > button.
- Set the parameters for the web service as follows:
where DateDifferenceCalculation.xslx is the name of the Excel workbook doing the date difference calculation.Parameter Value tns:workbookPath \\<your_server_name>\<your_trusted_file_location>\DateDifferenceCalculation.xslx tns:uiCultureName dataCultureName - Click twice on the Next > button.
- Uncheck the Automatically retrieve data when form is opened checkbox and click on the Finish button.
- Click on Add... to open the Data Connection Wizard.
- Select the Submit data option and click on the Next > button.
- Select the To a Web service option and click on the Next > button.
- Type in the URL to Excel Services running on your server.
Example: http://<your_server_name>/_vti_bin/ExcelService.asmx?wsdl - Click on the Next > button.
- Select the SetCellA1 web service and click on the Next > button.
- Set the parameters for the web service as follows:
Parameter Value tns:sessionId /dfs:myFields/dfs:dataFields/tns:OpenWorkbookResponse/tns:OpenWorkbookResult tns:sheetName /my:myFields/my:sheetName tns:rangeName /my:myFields/my:rangeNameStartDate tns:cellValue /my:myFields/my:startDate - Click on the Next > button.
- Rename the data connection to SetStartDate.
- Click on the Finish button.
- Click on Add... to open the Data Connection Wizard.
- Select the Submit data option and click on the Next > button.
- Select the To a Web service option and click on the Next > button.
- Type in the URL to Excel Services running on your server.
Example: http://<your_server_name>/_vti_bin/ExcelService.asmx?wsdl - Click on the Next > button.
- Select the SetCellA1 web service and click on the Next > button.
- Set the parameters for the web service as follows:
Parameter Value tns:sessionId /dfs:myFields/dfs:dataFields/tns:OpenWorkbookResponse/tns:OpenWorkbookResult tns:sheetName /my:myFields/my:sheetName tns:rangeName /my:myFields/my:rangeNameEndDate tns:cellValue /my:myFields/my:endDate - Click on the Next > button.
- Rename the data connection to SetEndDate.
- Click on the Finish button.
- Click on Add... to open the Data Connection Wizard.
- Select the Receive data option and click on the Next > button.
- Select the Web service option and click on the Next > button.
- Type in the URL to Excel Services running on your server.
Example: http://<your_server_name>/_vti_bin/ExcelService.asmx?wsdl - Click on the Next > button.
- Select the GetCellA1 web service and click on the Next > button.
- Do not set any parameters and click twice on the Next > button.
- Uncheck the Automatically retrieve data when form is opened checkbox and rename the data connection to GetDateDiff.
- Click on the Finish button.
- Click on Add... to open the Data Connection Wizard.
- Select the Submit data option and click on the Next > button.
- Select the To a Web service option and click on the Next > button.
- Type in the URL to Excel Services running on your server.
Example: http://<your_server_name>/_vti_bin/ExcelService.asmx?wsdl - Click on the Next > button.
- Select the CloseWorkbook web service and click on the Next > button.
- Set the parameters for the web service as follows:
Parameter Value tns:sessionId /dfs:myFields/dfs:dataFields/tns:OpenWorkbookResponse/tns:OpenWorkbookResult - Click on the Next > button.
- Rename the data connection to CloseWorkbook.
- Click on the Finish button and close the Data Connections dialog box.
- Double-click on the dateDiff field to open its Properties dialog box.
- Set its Default Value equal to the GetCellA1Result field of the GetDateDiff secondary data source, i.e., xdXDocument:GetDOM("GetDateDiff")/dfs:myFields/dfs:dataFields/tns:GetCellA1Response/tns:GetCellA1Result.
- Double-click on the button to open its Properties dialog box.
- Click on the Rules... button and add a Rule with the following Actions:
Query using a data connection: OpenWorkbookSubmit using a data connection: SetStartDateSubmit using a data connection: SetEndDateThe previous action should set the value of the sessionId field of the GetDateDiff secondary data source equal to the value of the OpenWorkbookResult field of the OpenWorkbook secondary data source.Set a field's value: sessionId = OpenWorkbookResultThe previous action should set the value of the sheetName field of the GetDateDiff secondary data source equal to the value of the sheetName field of the Main data source.Set a field's value: sheetName = sheetNameThe previous action should set the value of the rangeName field of the GetDateDiff secondary data source equal to the value of the rangeNameDateDiff field of the Main data source.Set a field's value: rangeName = rangeNameDateDiffQuery using a data connection: GetDateDiffSubmit using a data connection: CloseWorkbook - Close all open dialog boxes by clicking on the OK button on each open dialog box.
- Give your InfoPath form Full Trust by opening the Form Options dialog box via the Tools > Form Options... menu item, selecting the Security and Trust category, unchecking the Automatically determine security level checkbox, selecting the Full Trust option, checking the Sign this form template checkbox, and creating or selecting a digital certificate.
You should now have a fully functional InfoPath form with a button that when clicked on will calculate and display the amount of days between two dates entered. But you need not stop here; you can use the technique described in this article to leverage using any formula from Excel within InfoPath.
Related InfoPath Articles:
