Tuesday, January 17, 2012

Saving InfoPath Data to a Form Library

Using a data connection library and generated filename

Introduction
The default behaviour of a Form Library when a user saves the form is to prompt the user for a filename.

This is not desirable behaviour for a number of reasons, the two most important are:

1. The user does not know what is expected of them here
2. The filename will be random and could potentially overwrite other needed files

It is possible to automatically generate a filename using the tools built into InfoPath for the submit action, this works fine, but since the data connection is embedded, the form is harder to deploy to different environments.

Using a data connection library however allows simpler deployment to different environments, that is the process covered in this post. We will generate a filename based on the username and current date/time. This way, we can ensure that the filename will be unique. We will also build in rules associated with the submit button that ensure that an existing file is overwritten for updates, rather than a new filename being generated each time.

You must have design or higher permissions to the site that will host the data connection library and the InfoPath forms.  
Create Form Library
If you haven’t already, create a form library that will host your InfoPath Form.

1. Navigate to the site that you wish to add the form library (and data connections) to.
2. “Site Actions” -> “More Options”.
3. “Filter By” -> “Library”, select “Form Library”:

4. Name your form library and click “Create”.
5. Copy the URL of the form library and save it in notepad or similar.  
Create the Data Connection Library
We first need a place to store our data connections. This is the data connection library. Create this by:

1. Browse to the SharePoint site that you are going to create the library on.
2. “Site Actions” -> “More Options”
3. “Filter By” -> “Library”->”Data Connection Library”:

4. Name your Data Connection Library-> “Create”.
5. Copy the URL of the new data connection library into notepad or similar.  
Create a new Data Connection File
The data connection file itself is generated in InfoPath.
1. Open InfoPath Designer 2010, “Blank Form (InfoPath Filler)” -> “Design Form”
2. “Data” tab.
3. Click “Data Connections”:

4. In the data connections dialog box that opens up, “Add”.
5. “Create a new Connection to:”, select “Submit Data” (note, this if for our specific goal of creating a submit button that saves to this data connection, you can also choose to “receive data” if the purpose of this data connection was to populate the form with data from another list...).
6. “How do you want to submit your data” -> “To a document library on a SharePoint site”
7. Copy the URL of the FORM LIBRARY you created into the “Document Library” field. Delete any components of the URL after the document library name, for example, this URL:

http://yourWeb/sites/MySite/MyInfoPathLibrary/Forms/AllItems.aspx

Should become this URL:

http://yourWeb/sites/MySite/MyInfoPathLibrary

8. We are not going to generate a filename with this data connection at this time, the filename will be set by the InfoPath form itself. Leave this field as the default.
9. Click “OK”. You should see something similar to:  

10. Click “Next”. Enter in a name for you data connection, make it descriptive so that it’s clear by the name what the purpose of the connection is for. If this will be the only save connection to the library, you can also check “Set as the default submit connection”.
11. Click “Finish”.
12. We have created the data connection, we now need to save it to the data connection library we created. In the data connections window, select “Convert to Connection File”:

13. In the “convert data connection” dialog box, copy in the URL of your data connection library. Remember to remove any trailing information beyond the name of the library, so if the URL for your library is:

http:// yourWeb /sites/ MySite /DataConnectionLibrary/Forms/AllItems.aspx

Change it to:

http://yourWeb/sites/MySite/DataConnectionLibrary

Append the name of a new data connection file to this URL, resulting in a URL that looks like:

http://yourWeb/sites/MySite/DataConnectionLibrary/MyDataConnectionFile

The preceding URL will create a new data connection called “MyDataConnectionFile”. Click “OK”.

14. Once processing has completed, navigate to the Data Connection Library you created, you should see your data connection there:

15. Click the checkbox beside your data connection, and choose “Approve/Reject” in the office ribbon (or click the dropdown arrow beside the connection name and select “Approve/Reject” from there):

16. Choose “Approved” and click “OK”. Your connection is now available for use.  
Use the Data Connection in an InfoPath Form
Now that we’ve created the data connection and approved it in the data connection library, we can use that data connection in our InfoPath Form.

1. First create a field to hold the filename of the form. This field is necessary to save the file name once the user has submitted their first form. Otherwise, a new filename would be generated every time the user submits the form. If you don’t already see your form fields on the right of your InfoPath window, go to the “Data” tab, and click “Show Fields”.
3. Right click on the top level folder in your fields box and choose “Add”.
4. Enter in “strFileName” into the “Name” of the field, ensure it is a field of type “Text(string)”.
5. Click “OK”.

Now add the data connection
.
1. “Data” tab, click “Data Connections”.
2. Click “Add”.
3. Choose “Search for connections on a Microsoft SharePoint Server”.
4. Choose “select site” to see if your site is already listed in the “Site:” drop down list. If it is, go to step 9. If not, click “Manage Sites”.
5. Click “Add”.
6. Enter in the URL of the site that hosts your data connection library, for example:

http://myWeb/sites/MyForms.

7. Enter a display name.
8. Click “OK”.
9. You should see your site name in the list of sites. Click “Close”.
10. After a moment or two, you should be able to select your site from the list, and then see the name of your data connection library displayed in the dialog window:

11. Click the “+” sign beside the data connection library name.
12. Select the desired data connection from the resulting list.
13. Click “Next”. On the next screen, use the function button and select the "strFileName" field you just created.  This will give the form a dynamic filename based on the values we determine later.
14. Click “Next”.
15. Enter in the name for your data connection or accept the system generated name. Click “Finish”. The data connection can now be used by your form.  
Configure Save Button
The next step for this process is to create our custom save button and hook it up to the data connection we just created.

1. Open the InfoPath form you wish to add the save button to.
2. Add a button to your form (“Home” tab-> “controls”->”Button”).
3. Click on the button, then click “Manage Rules” in the office ribbon of the “Home” tab.
4. Click “New” and select “Action”.
5. Name your rule “Submit No Filename”.
6. Click the blue “condition” link.
7. Select “strFileName” as the first field value, and then “is blank” from the dropdown list on the second field:
12. Click “OK”.
13. In the “Run these actions” section, click “Add”.
14. Choose “Set a fields Value”.
15. Click the field select button and select “strFileName”.
16. Click the Function (fx) button and enter in a formula that will result in a unique filename. In our example we chose “concat(username(), “-“, now()”:


17. Click “OK”. This will set the strFileName value on the first form save. Now let’s save the form without prompting the user.
18. Again, beside “run these actions” choose “Add”.
19. Choose “Submit Data”.
20. In “Data Connection” choose your data connection added previously to this form for this purpose.
21. Click “OK”. This will submit the form to the form library. If desired, we can also close the form from this action (if this is a save button that allows partial saves, then this is not desired behaviour, if it is the final Submit action of the user, it is).
22. (Optional) Again, beside “run these actions”, choose “Add”. Select “Close the form”.
23. Click “OK”. Your rules editor should look similar to this:

24. Now we have set up the button for the situation when the form is first saved, a filename is generated and saved with the form. Now let’s prepare for the situation where the user is opening an existing file and we wish to overwrite the existing form data.
25. In the rules pane, click “New”, select “Action”.
26. Name your rule “Submit With Filename”.
27. Click the “condition” blue link and select “strFileName” from the first drop down, and then select “is not blank” for the second condition:

28. Click “OK”.
29. In the “Run these actions” section, click “Add” and select “Submit Data”.
30. Select your save data connection in the “Data Connection” section if it hasn’t been selected by default and click “OK”.
31. (Optional) Again, beside “run these actions”, choose “Add”. Select “Close the form”.
32. Your rules editor should now look like:

33. Save your form.
34. Publish your form to the form library created in the first part of this document (“Publish”->”Publish form to a SharePoint Library” -> “Form Library” -> “Update the form template in an existing form library” -> Select your form library).
35. Test your form by going to the form library and opening a new form. Save the form. The form data should now appear in your form library. Open the form again by clicking the saved data link, add more information, and save again. Ensure no new form has been created on the new save. Ensure that the information on the form has been updated.
Hide the Office Ribbon Buttons
Now that we have created a save button, we can hide the existing save buttons and other buttons that appear in the default office ribbon when a user opens an InfoPath form.

1. Open the your InfoPath form.
2. Click the “File” tab, and select “Info” if it isn’t by default.
3. Choose “Advanced Form Options”:

4. “Web Browser” should be selected by default. Remove the checkbox from “Show InfoPath commands in Ribbon or toolbar”:

5. If this form is also going to be opened in InfoPath form filler, click the “Filler Features” category and remove all checkboxes.

6. Click “OK”. The Office Ribbon buttons are now hidden.

2 comments:

  1. This is truly a "nugget", and like "nuggets" was very hard to find. :) But thank you nonetheless.

    ReplyDelete