Auto-Generating Filenames for InfoPath Forms

http://claytoncobb.wordpress.com/2009/06/20/auto-generating-filenames-for-infopath-forms/


Auto-Generating Filenames for InfoPath Forms – I see this topic come up a LOT on forums all over the place, so instead of continuing to answer the question time and time again, I am going to post a quick blog entry showing the steps.  I think this stuff is fairly simple, but it just doesn’t pop into your head initially.  Once you see it and understand it, you’ll think it’s simple, too.  Keep in mind that I focus entirely on SharePoint-based InfoPath forms and for the most part BROWSER-ENABLED forms.  So, it’s possible and likely that all concepts explained here will work for non-browser forms and non-SharePoint forms, but just remember that my perspective is mostly browser-enabled.  The basis for this topic is that the standard method for submitting InfoPath browser-enabled forms is for the user to click Save at which point the user is prompted to give a filename along with choosing to allow overwrite.  I really dislike this feature, because users have no concept of proper naming convention for the most part, and there’s no way to guarantee they create a unique or meaningful filename.  I also just don’t like the user getting prompted.  I prefer for the user to just click a button that saves the file to SharePoint with a dynamically-defined filename, closes the form, and takes the user back to the forms library.  Ok, so how do we do ensure these few things:
  • The filename must be unique
  • The filename must persist through subsequent edits of the form without the user having to do anything
  • The built-in toolbar buttons must be hidden
  • A custom button must be created to do it all
Here are the steps:
  1. Create the field for storing the filename
  2. Create the Submit data connection that uses this field
  3. Disable the browser-based toolbar functions
  4. Create the custom submit button
Create a hidden field for storing filename
Create a strFilename field (Text).  Do not set any conditional formatting, rules, or data validation on this.  Add it to the canvas for now and make it read-only (Fig 1).  This will be visible for testing purposes only.

Fig 1 – Create hidden field named strFilename
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 2).
Fig 2 – Creating the Submit data connection for the Form Library
Disable toolbar options
Click Tools > Form Options > Browser.  Uncheck Save, and Save As (Fig 3).  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 3 – Disabling toolbar functions for Browser-Enabled Forms
 Create the custom Submit button
Drag a button control to your canvas and double-click it to get to its properties.  Change the display name to “Submit” or whatever you prefer, then click Rules.  You want to add two rules, each with one condition (Fig 4).
 
Fig 4 – Custom Submit button with two rules
The first rule allows you to Edit the existing form without changing strFilename, then close it (Fig 5).
  • 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 5 – 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.
The second rules will Submit the current form only if it is brand new and after dynamically creating the strFilename, then it closes the form.
  • 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 6) to the concatenated string combining userName() with now().  The formula is concat(userName(), now()).  The Xpath is concat(xdUser:get-UserName(), xdDate:Now()).  Note: I add a text hyphen in the middle to make it a little easier to read, but that’s up to your discretion.
    • Submit to your SharePoint Library Submit data connection
    • Close
Fig 6 – 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 as long as it is unique.  This particular formula can only create a non-unique filename if the same user submits two separates forms (two browser forms open simultaneously) at the same EXACT second.
Publish your form to a new form library, and set that library to Display in Browser (Advanced Settings).  Click New to create a new form (Fig 7)
FN07
 
Fig 7 – A new form before submission
Click submit, and it should take you back to the library with no prompts or delays.  You will see a new form with a unique filename (Fig 8).  The now() function gives the date AND time separate by the letter “T.”
FN08
 Fig 8 – Newly-created form has unique filename
Click on that new form and notice that the strFilename field has been populated with the same info you saw for the filename in the library (Fig 9).  This field is actually populated FIRST when you submit, because it forms the basis for the filename in the Submit data connection.  Click submit again and notice that the existing file has been changed rather than a new file being created.
FN09
Fig 9 – strFilename field is populated with the dynamic, unique filename
Reference links for related articles:
InfoPath Submit Error- A value in the form may be used to specify the file name - You may be getting this error.   This blog post helps explain why, and my blog post helps explain how to overcome it.

Comments

Popular posts from this blog

SharePoint 2016 and 2019 Ports

PsConfig step by step /Configuration Wizard. “Upgrade Available” vs “Upgrade required”

Unlock the SharePoint site using Powershell command