Generating a unique form number


I’ve had someone ask me about automatically giving InfoPath forms a unique number. The scenario in question was one where the forms were being saved to a SharePoint document library with a unique number attached.


Documents in a SharePoint library do get given an ID, which is unique within document library. This can be used as the unique identifier of the forms out of the box with no additional work. The problem is, this only works so long as your forms remain in that document library. Once you start moving or copying forms (perhaps as part of a workflow) the ID will change.


In those situations, you will have to add a bit of work to your form to generate this number. You just have to be careful about when you generate it. If you have the possibility of multiple people filling out the form at the same time, you need to make sure the number is created in such a way that the method doesn’t result in forms being given the same number. If the form were to create this number on open, basing it on the number of files in the library, you would have issues when someone else opens the form template before the first form has been saved to the library. The easiest way round this issue is to have the number generated on submission.


Here I will describe a simple method of creating this auto number.


Firstly, you will need to create a data connection. If your forms (or a copy of them) are going remain in the document library they are submitted to, then you simply need to create a connection to retrieve data from that library. You only need to retrieve the data from one column. For the sake of argument, we’ll say the unique_number column. I’ll discuss another possibility in a bit for the scenario where forms will be moved or deleted from the library.


Set up the data connection using the wizard as you normally would, but uncheck the box when it asks whether to retrieve data when the form opens. You won’t want to get this information until the end. You will also want to set up the data connection to submit the form to the appropriate form library. Do this as you normally would, but make sure that one of your promoted fields is the unique_number field where you’ll be storing the unique ID of the form. From the submit options menu, uncheck the box labelled Show the Submit menu item and the Submit toolbar button. This is because you don’t want your users to just submit the form; you need the form to perform other actions first.


To allow your users to submit, add a button to the form. You’ll want this button to be set to trigger rules and custom code. Click on the Rules button, then Add, then Add Action. The action you want first is to query using a data connection. Choose the data connection you created to retrieve data from the form library.


 The second action is to set a field’s value. The field will be your unique_number field. Click on the function button next to the value field. Insert a function. You have a couple of different options here. You can choose to perform the count function on the items in any field, or the max function on the unique_number field. Either way, you should end up with the most recent unique number assigned. So now, you just add one to get your new one.

Photobucket

 


The third and final action you need to add to this button is to submit the form.


So there you have it. Your form will now check how many items there are in the form library, create a unique number based on that value and save the form back. Brilliant!

Photobucket

 


But you can probably see why this method only works for scenarios where you leave the forms in the library. If you have a process that involves moving the form from that library to another, the count value will almost certainly be wrong and the max value might well be. There are probably several different ways to work around this. One simple way is to create a SharePoint list to act as a counter.


Have a list with one numeric element. You’ll want to set this list up so that it’s not visible from the menus on the site since you’ll only want to access it through the form and workflows. Initially, this will have the value of 1. Create a one step workflow using SharePoint Designer and attach this workflow to the form library. The workflow should trigger every time an item is created in the form library and all it should do is add 1 to the value in the counter (using the calculate value and update list item inbuilt actions). Then your form should retrieve data from this list instead of the document library and use the counter value as the unique_number.

Comments (2)

  1. Anonymous says:

    Hello there,

    Thanks for the input this is the way I had my form already setup but I am having the issues when the form is moved!

    If possible could you go in to more detail with the last part of your post? in regards to creating a counter on a sharepoint list.

  2. werwre says:

    https://www.facebook.com/WatchWarRoomOnline
    http://www.screencast.com/t/9qQuJQqKp
    http://www.screencast.com/t/FGoxaanjA
    https://www.rebelmouse.com/WatchVacationOnline/
    https://www.rebelmouse.com/watchbridgeofspiesonline/
    https://www.facebook.com/WatchEverestOnline
    https://www.facebook.com/WatchSelflessOnlineNow
    https://www.linkedin.com/grp/post/6973703-6017397215125852162
    https://www.linkedin.com/grp/post/6973703-6017392948025507843
    http://www.screencast.com/t/TvvvXobmy
    https://www.facebook.com/WatchTrainwreckOnline
    https://www.facebook.com/WatchPaperTownsOnline
    https://www.linkedin.com/grp/post/6986867-6032617729138110465
    http://www.screencast.com/t/1JVrpAs1
    https://www.rebelmouse.com/WatchAmyOnline/
    https://www.facebook.com/WatchKitchenSinkOnline
    http://www.screencast.com/t/quSMVzunkfta
    https://www.facebook.com/WatchBeforeIWakeOnline
    https://www.linkedin.com/grp/post/6981021-6017294592175517699
    https://www.rebelmouse.com/WatchHitmanAgent47Online/
    https://www.facebook.com/WatchStraightOuttaComptonOnline
    http://www.screencast.com/t/naHd7EViG
    https://www.facebook.com/WatchMastermindsOnline
    https://www.linkedin.com/grp/post/6980115-6017737638633549828
    https://www.linkedin.com/grp/post/6986867-6032615843915915264
    https://www.facebook.com/WatchSouthpawOnline
    https://www.linkedin.com/grp/post/6980115-6017735463568158724
    https://www.facebook.com/WatchMinionsOnlineNow
    http://www.screencast.com/t/tzQwd7gc
    http://www.screencast.com/t/S2I3Wrr82Vi
    https://www.facebook.com/WatchVacationOnline
    https://www.facebook.com/WatchAntmanOnlineNow
    https://www.rebelmouse.com/WatchSinister2Online/
    http://www.screencast.com/t/KBqVeCR8
    https://www.linkedin.com/grp/post/8338032-6017245551668580353
    http://www.screencast.com/t/rWBrnCyXs
    http://www.screencast.com/t/5M6Ppcs2d
    http://www.screencast.com/t/8e9fKhj56
    https://www.facebook.com/WatchSinister2Online
    https://www.rebelmouse.com/watchthelastwitchhunteronline/
    https://www.linkedin.com/grp/post/6981021-6017293164958732291
    https://www.facebook.com/WatchAmyOnline
    https://www.facebook.com/WatchTheLastWitchHunterOnline
    https://www.rebelmouse.com/WatchTheGiftOnline/
    https://www.linkedin.com/grp/post/8337129-6017928009376362496
    https://www.linkedin.com/grp/post/6986867-6032763130319224832
    https://www.rebelmouse.com/WatchJaneGotaGunOnline/
    http://www.screencast.com/t/q6NCttkfqwH
    https://www.facebook.com/WatchCrimsonPeakOnline
    http://www.screencast.com/t/1tmJpsMuw

Skip to main content