Clayton's SharePoint Madness

All About SharePoint, InfoPath, and SharePoint Designer!

Auto-Numbering InfoPath Forms

Posted by Clayton Cobb on June 15, 2009

How do I avoid getting NaN when using the max() function? There is already a blog post referencing this, but people seem to be having trouble with it, so I am going to explain the steps in detail with screenshots.  Here are the steps that SYM Wong-A-Ton gave:

  • Change the RetrieveIDs data connection to use the autonumber field instead of max(@ID) + 1.
  • Add a Button to the form with the following 4 rules:
    • Query the RetrieveIDs data connection
    • Set the autonumber field equal to 1 when count(@ID) = 0
    • Set the autonumber field equal to max(@ID) + 1 when count(@ID) > 0
    • Submit the form to the form library
  • Disable the submitting of the form via the Submit button on the toolbar (the Button you added will submit the form instead)

These steps make sense, but you have to know how to actually configure your button settings to match the words.  I think some more steps are needed, so here is my outline:

  1. Create RetrieveIDs data connection
  2. Create hidden fields for storing the ID and Filename
  3. Create a Submit data connection for your that uses Filename field for submit filename
  4. Disable toolbar options
  5. Create custom button with 5 rules

Create RetrieveIDs data connection Create a RECEIVE data connection in your form template that connects to the Form Library where the form resides.  Unless you want other metadata from this library, only select the ID field when configuring the DC.  On the last page, make sure to deselect the checkbox for “Automatically retrieve data when form is opened.”  We want to retrieve the data at the exact moment before submitting the form just in case someone else has submitted a form while our form was open (Fig 1).


Fig 1 – Data connection to retrieve IDs from the SharePoint List 

Create hidden fields for storing the ID and Filename Create a numID field (Whole Number) and strFilename field (Text).  Do not set any conditional formatting, rules, or data validation on these.  Add them to the canvas for now and make them read-only (Fig 2).  These will be visible for testing purposes only.


Fig 2 – Create hidden fields for ID and Filename

Create a Submit data connection that uses the strFilename field for dynamically creating the filename Create a SUBMIT data connection in your form template that connects to the Form Library where the form resides.  Put in the URL of your Form Library for the “Document Library” field, and choose the strFilename data element for “File name” field by using the fx button.  Check the box for “Allow overwrite if file exists” (Fig 3).


Fig 3 – Creating the Submit data connection for the Form Library

Disable toolbar options Click Tools > Form Options > Browser.  Uncheck Save, and Save As (Fig 4).  Submit should be grayed since it has not been configured.  If it has been configured, be sure it is unchecked.  Update should be unchecked by default, so leave it.  I personally uncheck “Views,” since I use views to dynamically route people to certain information based off their identity or the form’s workflow status, but it’s up to you.  If your form is not browser-enabled, use the Open and Save menu in Form Options to uncheck Save and Save As.


Fig 4 – Disabling toolbar functions for Browser-Enabled Forms

Create custom button with 5 rules


Fig 5 – The full view of the 5 Rules

  • 1) Query the RetrieveIDs data connection (Fig 6)


Fig 6 – Querying the RetrieveIDs data connection

  • 2) Set numID to the the next incremental ID only with two conditions - there is at least one form already existing in the library and only if the current form has not already been submitted (Fig 7)
    • Set a condition rule to have two conditions
      • For the first, click “Select a field or group,” select the RetrieveIDsdata source from the top pulldown, drill down to the IDdata element, and choose “Number of occurrences of ID” in the bottom pulldown.  Select the operand “is greater than” and type the number 0 in the last box. 
      • For the second condition, simply choose strFilename in the first pulldown and set the operand to “is blank.”


Fig 7 – Setting the incremental ID with conditions

  • Create one action that sets numID to the next number higher than the highest ID in the form library (Fig 8).  For the Action, choose “Set a field’s value.”  For the Field, choose numID.  For Value, click the fx button and put in the formula max(@ID) + 1(do not copy and paste this – you must use the Insert Function and Insert Field buttons to make sure it resolves properly.  You can also copy this Xpath string: xdMath:Max(xdXDocument:GetDOM(“RetrieveIDs”)/dfs:myFields/dfs:dataFields/dfs:AutoNumbering/@ID) + 1


Fig 8 – Setting the ID to the next incremental value

What we are accomplishing here is that we are giving our current form the next ID in line before submitting the form.  We are making sure this rule only runs when the library is not empty, because it would cause an error when there is no ID to use for the max(ID) function.  Also, we are only setting this value when the form is brand new, which means it has no strFilename yet.  We have to do this so that if the form is edited, there will be no change to the numID field.  We only want this rule to run when the library is not empty and when the form is brand new.

  • 3) Set numID to 1 upon the single condition that the form library is empty (Fig 9).
    • Set a condition rule to have one condition.  Click “Select a field or group,” select the RetrieveIDsdata source from the top pulldown, drill down to the IDdata element, and choose “Number of occurrences of ID” in the bottom pulldown.  Select the operand “is equal to” and type the number 0 in the last box.


Fig 9 – Setting the Initial ID with a condition

  •    
     
    • Create one action that sets numID to 1 (Fig 10)


Fig 10 – Setting the ID to 1 for the first form in the library

What we are accomplishing here is that we are giving our form the first ID of “1″ due to the fact that it is the first form to be submitted to the library.  This is only possible when the form is brand new, so there is no need to add a condition related to the strFilename.  All we need to do is check to see if the form library is empty.  This step circumvents the NaN error when using max(ID) and getting a null value back.  Also, notice that this rule is put in line AFTER the Set Next ID step above.  The reason for this is because both rules would run if they were in the opposite order, and you’d be skipping IDs.

  • 4) Edit the existing form without changing numID or strFilename, then close it (Fig 11).
    • Set a condition rule to have one condition stating that strFilename is not blank.
    • Add two actions in this order:
      • Submit to your SharePoint Library Submit data connection
      • Close


Fig 11 – Editing the form with the submit data connection

Here, we are simply submitting the form back to the library in a manner that will not change any metadata and will overwrite the existing file of the same name.  This is the whole reason for the strFilename field.  We do not want the strFilename to be recreated each time the form is edited.  This rule is placed before the Submit rule for the same reason as stated above.  We need to check first to see if the form has already been submitted.  If so, then we submit using the current strFilename.  If not, then we skip this rule.

  • 5) Submit the current form only if it is brand new and after dynamically creating the strFilename, then close it.
    • Set a condition rule to have one condition stating that strFilename is blank.
    • Add three actions in this order:
      • Set the Value of strFilename (Fig 12) to the concatenated string combining userName() with the numID field.  The formula is concat(userName(), numID).  The Xpath is concat(xdUser:get-UserName(), my:numID).
      • Submit to your SharePoint Library Submit data connection
      • Close


Fig 12 – Setting the dynamic filename prior to submission

Here, we are submitting the form to the form library for the first time.  This is why we first dynamically create the strFilename, because the Submit data connection uses this field to create the filename in SharePoint.  We only want to do this step upon first submission, so that is why this rule only runs if the strFilename is blank.  Doing this rule last keeps us from double-submitting, because the Edit rule would run right after the Submit rule due to its condition being met (strFilename is not blank).  You can of course choose any concatenation formula you want, or you can use no formula and simply use the numID value – this part is up to you. After publishing the form template, go to your library, which should be empty with no files ever having been submitted (Fig 13), otherwise the ID of the actual files in the library won’t start with 1, and the InfoPath numID won’t match the SharePoint ID.


Fig 13 – In your new Form Library with no history of records, click New

The new, blank form should look like this (Fig 14):


Fig 14 – Blank version of the form

Click Submit, and you should be returned to the library showing just one file (Fig 15), and it should have the Name of username1 (i.e. ccobb1).


Fig 15 – First list item is correctly given the ID of 1

Click on that file to re-open it, and it should show the populated fields with numID = 1 and strFilename = ccobb1 (Fig 16).


Fig 16 – Form data fields show proper ID and Filename after submission

Click Submit again, and notice that you go back to the form library, but nothing has changed on the form except the Modified date/time stamp.  The ID did not increment, and a new file was not created, because we were just editing the same file.  Now, click New again, and submit.  You should see a new file with a Name that is equivalent to ccobb2.  Do this several times and edit some of those forms several times to ensure they all behave properly. There is one big problem with all of this, though, and I’ll demonstrate it for you.  This entire concept works well UNLESS all the forms get deleted so that there are no forms remaining.  If you do that, then the next form you create will be given the numID of 1 and the strFilename of ccobb1.  However, in SharePoint, the ID will be the next highest ID in succession after the highest ID that had been created and subsequently deleted.  Deleting files in a SharePoint list does not re-start the ID increment, so keep that in mind.  If you delete all but one of your forms, then the numID solution we have here continues to work.  Here is an pic showing what happens if you delete all but one of the forms and then create a new one (Fig 17).


Fig 17 – AutoNumbering continues even after records have been deleted as long as there is at least one record in the library

Three other major concerns are the scalability issue, the View settings issue, and the heavy use issue.

  • Scalability – The data connection is pulling down the ID of every form in the main view of your form library.  What if you have hundreds or thousands of forms?  You will be pulling down tons of info that will slow the form load when all you need is a single piece of info – the highest ID in the list.  A potential workaround for this is to create a custom view that is set to Sort by Created in Descending Order with a finite Item Limit of 1 (Fig 18).  This always show only ONE item, and that will be the item with the highest ID in the entire list, because IDs are assigned upon creation of the file, so the most recently created file will have the highest ID.  The problem then becomes that you need to create a data connection directly to this view instead of the library itself (DCs to SP lists use the default view).  To do this, you must create an XML data connection pointing to the XML view of that SP view by using this write-up: Populating Form Data from SharePoint List Views.  Believe me, this works, and I use it all the time, but I can imagine if it seems daunting to people who are just trying to figure out the entire solution above.  To be clear, this DC would replace your RetrieveIDs DC from step 1.


Fig 18 – View Settings for showing only the list item with the highest ID#

  • View Settings – Similar to the above issue, the Max(ID) function is only going to return the highest ID that InfoPath sees in the default view.  The default view settings for a form library are to limit the items to 100.  Also, any filtering on the default view would potentially cause InfoPath not to see every ID.  You either have to make sure your default view has EVERY list item visible with no Item limit, or you need to use the above alternative view that shows only the highest ID form.
  • Heavy Use - By heavy use, I mean that new forms are being created very frequently – enough that there is the potential for multiple people to submit new forms within the same moments (the window of time depends on the speed of the system), because this method relies on being able to read the items in the library to get the current Max(ID).  If both get submitted close enough together, there is a chance that they assign themselves the same ID or that the submit fails altogether.  It’s best to submit the doc, retrieve the ID after submission, then submit again (all in one button push), but this only works if you don’t need the ID itself in the filename.

40 Responses to “Auto-Numbering InfoPath Forms”

  1. sameel said

    Thanks a million.
    Works perfectly.

  2. maleesha said

    Clay,
    This is the best Sharepoint blog out there, and I have read them all. I will definitely be back often!

  3. Stella Goranova said

    Hello Guys,

    Please tell me:
    1. Do I definatelly need an XML schema doc in order to connect the Infopath form with the Sharepoint Library?
    2. Can you say/describe something more about using the Submit button. I got Error messages all the time and honestly do not know where the problem can be.

    Thank you!
    Stella

  4. Clayton Cobb said

    Stella,
    1) You do not need to make an XML schema. That is one method for creating a template – you can have a pre-defined XML schema that you set as your main data source when creating the form. However, none of my examples on this blog mention that. Were you talking about something else maybe? When initially creating your template, you can simply choose “blank,” which has no schema, but when you start adding data elements to it, the XML schema gets built for you. So, in essence, your InfoPath template (XSN) is really an XML schema that is built to work with an associated XML file that gets created after you submit a form based on that template. You don’t have to worry about all of that really, because you can just use the GUI to build it.
    2) There is a built-in submit button that I am not using in my example. I just drag a button over to the canvas, double-click to get its properties, then click Rules to tell it what to do. I do not change the button type to Submit, and I don’t use the built-in Submit button. Even so, any method of using a submit button should work, so I’d need to see your errors to help. You can email me your template at warrtalon@gmail.com if you’d like.

    • Stella said

      Hello,
      thanks for the answer.
      Indeed, I used/tried both approaches: created an XML Schema and then I decided I need to play around and did it from scratch without the schema.The problem is that I used the drag and drop button for the SUBMIT. And when I call/open the already connected Form to the SharePoint is gives me an error message. I checked the folders, the names.. and the last message was that there is a missing folder NaN.xml in the Form library where the results to be stored. Here comes my confusion.. Can you help me? Probably I guess I miss smth in the process or need to create a folder?!Hm..

      thanks in advance!

      • Clayton Cobb said

        Stella, This should not be difficult, so go to my Contact page and send me an email with your form template (XSN).

  5. Duncan said

    Thank you, it works!

    • Clayton Cobb said

      Duncan, glad to help. I guess when people just say thanks and don’t have a lot of confusion, then it must be easy to follow. Any comments along those lines would be very helpful…

  6. You rock Clay! Keep up the nitro-powered MVP acceleration.
    Your biggest fan,
    Patrick

  7. Joyce Hughes said

    I have searched high and low for the answer to this problem. Thanks to Patrick at InfoPathDev.com for pointing me in the right direction. This works amazing. The only thing is that I have an electronic signature on the form and when they sign it, they can no longer insert the ID upon submission. Any other thoughts?

    • Clayton Cobb said

      Joyce, are you using the Qdabra Electronic Signature solution? How exactly does the user “insert the ID” upon submission? this may require some back-and-forth, so just email me the details, and we can get Qdabra dev help if it’s related to their solution.

      Also, I thought you were referring to the Auto-Generating Filenames solution, so I sent the wrong pics. I will send you the pics from this blog (18) now.

  8. Joyce Hughes said

    Hi Clay,

    Sorry it took so long to respond Clay. I am in a class this week. Thanks for the pics, I received the correct ones.

    I am using the signature control that is available in the Infopath Form Application. I don’t know if it is Qdabra. I send the form by email to the manager who clicks on the link. The browser form displays and he/she is able to electronically sign with their CAC card. Then he/she clicks on the submit button. Because the form is already signed, it becomes read only so the ID cannot be inserted into the form. The problem is that the final receiving end of this form needs the ID to be assigned to the form. I think you are right about the back and forth, but I don’t know how to do it.

    Thanks for your excellent and prompt help Clay.

    • Clayton Cobb said

      Ah, I see exactly what you mean:
      1) You said electronic signature, which confused me into thinking it was the Qdabra solution. It’s just semantics really, but what you are using is a true DIGITAL signature. This is backed with a real security token, which is a CAC card for you. An electronic signature is a step below and doesn’t use a true secure solution but is still electronic. Check out the electronic solution on Qdabra’s site or InfoPathDev (same company) and see if that may meet your needs. If not, and you require the true digital sig, then we’ll go that route.

      2) You say it makes the whole form read-only? Doesn’t the digital signature only lock the section(s) that you define? Can’t you put your whole form editing area in a section and only lock that section so that the ID concept I have in my blog post can still be used with hidden fields? Whichever way you go, the digital signature should not lock the entire form unless you require it to be so…

  9. Joyce Hughes said

    Maybe I am doing it wrong (setting up the digital signature) to sign only a part of the section. I thought that is what I did, but maybe that is the problem. This is what I have set up:

    The group that includes all of the data is in Group 1.
    In the section properties of the digital signature, I have the following checked:
    Allow users to digitally sign this section
    Sign the following data in the form when this section is signed (Group 1)
    Show signatures in the section

    Also, I have the control ID in its own section that includes your instructions. If I submit the form without the signatue, it works slick.

    If I sign it then submit, I get an alert that says “Some rules were not applied.” I click on Show Details an get the following message “This value has been digitally signed and cannot be changed.

    Am I setting up the digital signature correctly?

  10. Joyce Hughes said

    Joyce <<<======== Jumping for joy!

    I figured it out. The problem was that I was setting the params for the digital signature to sign Group 1, and it still didn't work.

    So…I was playing around and set the digital signature to sign its own section(in this case group 7) I was then able to sign, then submit and the ID number then was populated into my form.

    Yea!!!! Thanks for your help Clay.

    • Clayton Cobb said

      Sweet! Glad you figured it out before I even had time to dig into the form. Saves me some time for sure! =P Also glad all of my input has helped move you along – that’s the whole point of what I do. =D

      Just make sure that only signing group 7 is ok, because you don’t want your “signed” data to be modifiable after signing.

  11. SuperScott said

    Hey Clay thanks for your work on this.

    Is there any way you can repost the pictures on your wordpress site, because at work photobucket is blocked.

    I’m pretty sure i’m doing this right, but i think i’m losing something in the translation.

    =D

    Thanks,
    -Scott

    • Clayton Cobb said

      Scott,

      I created this one before I learned how to use Word to publish blog posts. I no longer need to host pics elsewhere, so I will download this blog post with Word and then re-publish from Word, which saves the pics directly into my WordPress account. I’ll do that later from home, since it’s not working from the office.

      -Clay

  12. Lalith said

    When i trying to click submit button following error come
    what is the reson?

    InfoPath cannot submit the form.
    An error occurred while the form was being submitted.
    One of the required values for submitting the form is missing. The value is created using the following XPath expression: my:strFilename

    If you know the value in the form that specifies this value, revise it and try again.

    • Clayton Cobb said

      Lalith, strFilename must have a value. The error is telling you that strFilename is a required field but has no value in it. You need to populate strFilename dynamically using my technique in Figure 12. If you did that step, then it must be incorrect.

  13. Wesley said

    Hi guys!!!

    I need to store the number generated to continue, because when I close the form the ID start from begin (0)!

    When I close the form I need that he star from the last number generated.

    Someone to help me???

    • Clayton Cobb said

      Wesley, could you explain your problem in more detail? I do not understand what you are saying. Are you closing the form or submitting it? Just closing the form does nothing. If you follow my instructions, then when you submit the form, it will always use the next ID in sequence.

      • Wesley said

        Hi Clayton,

        So, I submitted my form, but when I open to submit a new form, is showed a new ID, but is 0 again! lool

        He don’t search the higher number to increase it, it always start from 0.

      • Clayton Cobb said

        It should never be 0 to begin with. In my write-up here, the ID is never 0. If there are no items in the list, then it automatically sets the first ID to 1. After the first one is submitted, it always uses the next ID in line. Why are you getting 0? At what point in my write-up does it cause the ID to be 0?

  14. Wesley said

    Clayton,

    I will explain all my code to you!!!
    “Id” is a fild that the SharePoint add a number automatically when I create a new form.
    “controlnumber” is a fild that I need the user see when she fill the form, so I created a button to generate a new control number, and imputed the rules.
    I have in my rules:
    Query ID (conform you explain);

    SetNext ID (ID > 0)
    ControlNumber = max(@ID) + 1

    Set Initial ID to 1 (ID = 0)
    @ID = “1″

    • Wesley said

      I’ll be back tomorrow.
      Tks a lot for your help today!!!

    • Clayton Cobb said

      Are you actually doing this with code? I don’t follow your logic. Is there a reason you didn’t follow my steps? My steps work for sure, and there is no code involved.

      • Wesley Augusto said

        This that I wrote is this code that you write on the button.
        ◦Query the RetrieveIDs data connection
        Query ID (conform you explain);

        ◦Set the autonumber field equal to 1 when count(@ID) = 0
        My code:
        Set Initial ID to 1 if (ID = 0)
        @ID = “1″

        ◦Set the autonumber field equal to max(@ID) + 1 when count(@ID) > 0
        SetNext ID if (ID > 0)
        ControlNumber = max(@ID) + 1

        ◦Submit the form to the form library
        I created a other button to submit the form, because I need the user view the number generated before to submit de form, because this number will be used to fill out the an e-mail.

        When I submit the form all filds were sent to the sharepoint, the button submit is ok.

        I think this is the connection that isn’t reading the ID from the Infopath field or the search the max number to increase +1 is bad.

        When I said generate number, it generate starting from the number 1, and when I click on to button generate again it show number 2, the problem is that isn’t save the number and when I open the form again it don’t start from the last one generated.

        I hope have cleaned the explain….

        Tks so much for you help!!!

      • Clayton Cobb said

        Your logic just isn’t correct. You don’t need to write code for any of this, and if you follow my steps, it works. That’s all i can say. I don’t know what you’ve done wrong. I don’t see why you need code nor why you need to show the number for an email. I would just automatically generate the email with the number – no reason to display it to the user for manual use. If you do that, you run the risk of another form getting submitted while you’re waiting, which throws off the IDs. You also have to make sure your default view in the library shows all the forms so that you can see all the IDs. That is in one of my warning.

      • Wesley Augusto said

        These isn’t a code is the steps from the figures 6, 7, 8 and 9.

        Do you have MSN… Can you add me?? wesleyaugusto@gmail.com

      • Wesley said

        Clayton!!!

        I could do it, but following other way, but with your considerations!!!

        Tks and regards for your help!!!
        Your forum is the best!!!

  15. Mark said

    Any idea why i get this error when i try and submit the form?

    The query cannot be run for the following DataObject: RetrieveIDs
    InfoPath cannot run the specified query.
    You are working offline. InfoPath will use offline data instead of connecting to external data sources. If no offline data is available, some form elements, such as drop-down lists, may be blank.

  16. Mark said

    ok i think i found reason.. i did not include document library name in the url for the submit…

    however on submitting im not seeing any forms in the document library.. the form closes and to all appearances has submitted, but nothing is listed in the library so cant tell if the id/filename is working ok

  17. Bil Simser said

    @Clayton: Nice job on the post and well documented. There is a problem with this approach however. Add 10 forms to a library so the next ID would be 11. Delete 5 of them. This solution will return 6, not 11 but when it actually saves it the name would be cobb5 but the ID field would be 11.

    I’m currently trying to retrieve the values of created, author, and ID when a user clicks on Save in a browser form (which just does an auto-filename then submits the form to the library). If I open the form up the values are there and can be used in an email I send out however if I try to send that email out on the first try the values from the form library are not there. Still scratching my head on this one.

    Thanks!

    • Clayton Cobb said

      Bil,

      Why do you think my solution will return something other than the actual ID? I pointed to this exact situation and showed how my method deals with it. It should always go with the ID – I even gave a screenshot of it in Fig 17. My solution does continue to fall in line with the proper ID unless you delete every record from the library, because then there is no way to know what ID is next. None of this matters, though, if you don’t need the ID in the filename. If you just need the ID in the form, then you don’t have the same limitation and can always do a double-submit. It’s the fact that some people need the ID in the filename that requires us to perform the logic PRE-SUBMIT, because once you submit once, the filename is set – it can’t be changed after that.

      How are you clicking Save and doing an auto-filename? Do you mean you are clicking Submit on a custom Submit button that you named Save? The built-in Save can’t do what you describe. Anyway, you aren’t seeing values when trying to send an email initially, because the form has to submit to the library then be retrieved back into the form before you do the Submit to Email piece.

      • Bil Simser said

        @Clayton: Maybe I’m missing something (just built it on my system to confirm). If you create a 5 items and delete the last 4, the next one you create will have numID set to 2 but the ID number is 6. In my situation I had to a couple of rules to adhere to. 1) don’t reuse ID numbers and 2) send a notification out with the ID# of the form submitted.

        Your solution works for autonumbering if you want the next highest number based on the last item submitted (if the last item had numID = 20 but the ID was 100 the next numID and name would be 21). If you need the real ID you need to do a retrieve after the submit which seems to always set the numID to match the real ID (at least from my testing).

        I did it through something similar but used a query filtered based on the filename (which I set in a similar fashion based on whether it’s set or not). I have custom Submit (i.e. save and email a notification) vs. Save (just save the form as a work in progress). Mine works using the filter but I might switch it to what you have here as it might be a little cleaner.

        In any case, I think we’re talking about the same thing but thanks for the post. It’s very informative and great to have a resource like this for this sceanario.

  18. Clayton Cobb said

    Yeah, I know exactly what you’re talking about, but I had tested that scenario, and it still worked. What I hadn’t done is delete the most recent file, which then does screw it up. You can delete a whole bunch of records as long as the most recent one doesn’t get deleted, so that is an issue.

    The thing you mentioned where you retreive the ID after submission does not work for my scenario, because this article was for people who MUST have the ID in the filename. You can’t submit the form without a filename, so if you only retrieve the ID after it’s submitted, then it’s too late to put the ID in the filename. If you only need the ID in the form’s XML for logging purposes or email purposes, then it’s very easy, and none of this matters. The article is mainly for folks who need to get the ID in the filename, and doing so without code requires that the form developer be aware of certain limitations.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>