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

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.

Problem

You want to automatically assign a number to an InfoPath form whenever it is submitted to a SharePoint form library.

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.

Caveat: When the form library is empty, i.e. when it contains no forms, the max() function will fail and the resulting number for the first form that is submitted to the form library will be NaN. The correct numbering for subsequent form submissions should succeed. As a workaround you can manually rename the first form to 1 in SharePoint.

Discussion

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 (browser-compatible) 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 fields and then check the ID field. In the last step of 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 nodes, 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 resemble: 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 a field's value: autonumber = max(@ID) + 1 Note: @ID is selected from the RetrieveIDs secondary data source in the same way you selected it in step 8.
  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 currently open.
  20. Publish the form template to the library you created in step 1.

You should now be able to fill out a form and submit it. The next available number in the form library will be automatically saved in the autonumber field of the form when the form is submitted and this number will also be used for the Name of the form. This solution works for both forms that are filled out through the InfoPath client application as well as those that are filled out through the browser.

 
 Subscribe for updates via RSS or Email

Related InfoPath Articles:

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services