Saturday, August 7, 2010

Auto-numbering InfoPath forms when they are submitted to a SharePoint form library

Problem: You want to automatically assign a number to InfoPath form whenever a new form is submitted into form library?

I know this sounds strange - but this isn't easy thing to setup in form library - as you will see in few moments from the text below.

Solution: Use rules, formulas, and the max() function to generate incremental numbers that can be assigned to InfoPath forms when they are submitted to a form library.

You can accomplish this functionality as follows:
1. In Sharepoint, create a form library to submit your forms to
2. In InfoPath, create a new InfoPath form template
3. Add a Text Box control to the form template and name it autonumber
4. On the Tools menu click Data Connections
5. On the Data Connections dialog box, click Add, and create a Receive data connection to the Sharepoint library that you created in step 1. In step 5 of the Data connection wizard, uncheck all field and then check the ID field. In the last steop od the data connection wizard, uncheck Automatically retrieve data when form is opened, name the data connection RetrieveIDs and click Finish.
6. On the Data Connections dialog box, click Add, and create a Submit data connection to the Sharepoint library that you created in step 1. In step 3 of the data connection wizard, click the formula button behind the File name field.
7. On the Insert formula dialog box, type max( and then click Insert Field or Group.
8. On the Select a field or Group dialog box, select RetrieveIDs from the Data Source drop-down list box, expand all the nodex, click ID, and then click OK
9. On the Insert Formula dialog box, type ) + 1 and click OK.
10. On the Data Connection Wizard dialog box, the expression in the File Name field should look like:
max (@ID) + 1
11. On the Data Connection Wizard dialog box, click Next, name the data connection Main submit, and then click Finish
12. On the Data Connections dialog box, click Close
13. On the Tools menu, click Submit Options
14. On the Submit Options dialog box, check the Allow users to submit this form checkbox, select the Perform custom actions using rules option, click Advanced, select Close the form from the After submit drop-down list box, and then click Rules
15. On the Rules dialog box, click Add
16. On the Rule dialog box, add an Action that says:
Query using data connection: RetrieveIDs
17. On the Rule dialog box, add a second Action that says
Set field's value: autonumber = max(@ID) + 1

Note: @ID is selected from the RetrieveIDs secondary data source.

18. On the Rule dialog box, add a third Action that says:
Submit using data connection: Main submit
19. Click OK when closing all dialog boxes that are open.
20. Publish the form template to the library you created in step 1.

That should do it. Please try.