InfoPath Solutions Blog

Blog about InfoPath and related technologies such as SharePoint, .NET, and other Microsoft software products.

Top 10 questions about InfoPath file attachments

Published on Tuesday, 6 April 2010 by S.Y.M. Wong-A-Ton

In the past months (and year), I’ve written much about how to use File Attachments in InfoPath – I’ll sprinkle links to some of those articles throughout this post.

Recently I spent some time going through the search queries on BizSupportOnline.NET and came up with the following list of the 10 most popular queries about InfoPath file attachments.

1. Where are InfoPath form attachments stored?

InfoPath form attachments are stored as pieces of text called base64 encoded strings within the InfoPath form itself.

To be able to store a file as a string, InfoPath converts the binary data of the file to a string. This is called encoding.

Once you’ve added a file as an attachment to an InfoPath form, you can use the menu options Open or Save As on the File Attachment control to retrieve the file that was stored in the InfoPath form.

When you select either menu option, InfoPath converts the string it stored previously back to binary data and presents you with the file again. This is called decoding

2. Multi file attachment InfoPath

You can only store one file in one File Attachment on an InfoPath form; you cannot store multiple files in the same File Attachment field.

To create a multi file attachment control in InfoPath, you can make use of a Repeating Table that has a File Attachment as one of its fields.

Perform the following steps to create a multi-file attachment in InfoPath:

  1. Add a Repeating Table with 1 column to your InfoPath form template.
  2. Go to the Data Source task pane and double-click the field under the repeating node for the Repeating Table.
  3. Change the Data type to Picture or File Attachment (base 64) and click OK.
  4. Right-click the field in the Repeating Table on the InfoPath form template, and select Change To, and then File Attachment from the context menu that appears.

Now you’ve got yourself a multi-file attachment in InfoPath.

3. Validating event of a File Attachment control InfoPath

You can use the Validating event of InfoPath file attachments to validate things such as file names, file types, or file sizes.

For examples, see:

4. How to encode and decode a File Attachment programmatically by using Visual C# in InfoPath

While InfoPath takes care of this automatically for you when you add and retrieve attachments in InfoPath, you can also write code to do these conversions yourself.

The following figure shows the encoding and decoding stages of an InfoPath file attachment.

Encoding and decoding an InfoPath file attachment

Figure 1. Encoding and decoding an InfoPath file attachment.

Microsoft has published a KB article on how to encode and decode InfoPath file attachments.

5. InfoPath submit attachment

Attachments are technically speaking never submitted to anywhere. They are part of an InfoPath form, and the InfoPath form is submitted to somewhere.

Having said this, you can extract attachments from an InfoPath form, and store them for example in a SharePoint document library or as attachments to SharePoint list items.

For examples, see:

6. SPD custom action to extract InfoPath attachments and upload to SharePoint

You can also use a custom SharePoint Designer action to extract InfoPath file attachments and then upload them to SharePoint either as attachments in a SharePoint List or documents in a SharePoint Document Library.

I created the following 2-part video tutorial that walks you through the process step-by-step:

7. Code to rename InfoPath File Attachment

To rename an InfoPath file attachment, you first have to decode it, change its file name, encode it again, and then save it.

For an example, see How to programmatically rename an InfoPath file attachment.

8. Delete file attachments in InfoPath programmatically

You can delete an attachment either manually or programmatically in InfoPath; manually by using the Remove menu option on the File Attachment control, and programmatically by adding a nil attribute to the field.

The following article goes into more detail: Delete, remove, or clear an attachment from an InfoPath form.

Note: Deleting an InfoPath attachment is the same as clearing the File Attachment control.

9. File types for InfoPath attachment control

You can set the file types an InfoPath attachment field should accept by going to the Properties dialog box for the File Attachment control, selecting Allow the user to attach only the following file types, and entering the file types you want the user to be restricted to.

If you’re using InfoPath browser forms, then this option is not available and you’ll have to programmatically restrict file types for InfoPath attachments.

10. Loop through InfoPath file attachments in a workflow

To loop through InfoPath file attachments in code, you can use an XPathNodeIterator object.

I demonstrated this technique in Upload images from InfoPath to SharePoint using a custom SPD activty – Part 1.

You can locate the code for looping through InfoPath file attachments by pausing the video 5 minutes and 19 seconds into the demonstration.

InfoPath Basics: InfoPath Resource Files – purpose and benefit

Published on Monday, 29 March 2010 by S.Y.M. Wong-A-Ton

A heavily underused and probably misunderstood feature of InfoPath is the concept of resource files.

This is the description of a resource file – directly quoted from Office Online:

A resource file is a file that is used to display additional information in a form template. When you save or publish a form template, any resource files are included in the form template.

The main thing to remember about resource files is that they are stored within the InfoPath form template itself.

And a major benefit of this is that you can move an InfoPath form template from computer to computer without having to worry that you’re breaking data connections.

A best practice is to use resource files whenever you have external files your InfoPath forms need to access. By adding such files as InfoPath resource files, you remove the dependency of needing access to external locations, which may become unavailable from time to time.

You can add resource files directly to an InfoPath form template via the Tools > Resource Files menu option, but you can also have InfoPath automatically add a resource file for you when you select certain options when creating a data connection to an external file.

For example, you can create a Receive data connection to an XML file and tell InfoPath to access the XML file from a location on disk and not to store a copy of the data in the form template. When you do this, a resource file will not be created and stored within the InfoPath form template.

But you can also tell InfoPath to include the data in the InfoPath form template upon which InfoPath will include it as a resource file.

The best way to see how InfoPath stores resource files is to create a data connection to an XML file that is located somewhere on disk, accept all of the default options which are presented to you in the Data Connection Wizard, and then go to Tools > Resource Files to see that a resource file has been automatically created for you.

You can then verify that a resource file is indeed part of an InfoPath form template by extracting the InfoPath form template files via the File > Save As Source Files menu option.

Display the same repeating table in different places in InfoPath

Published on Tuesday, 16 March 2010 by S.Y.M. Wong-A-Ton

There might be times when you would like to enter data in a repeating table on an InfoPath form, and then take this data and display it in another repeating table either on the same View or a different View.

I’ve previously created an InfoPath video tutorial that explains how to work with Views in InfoPath.

A View is not only a canvas on which you place controls, but you can also see it as a window into the data source of an InfoPath form template.

This means that you can have multiple controls, which include repeating tables, on a View pointing to the same data in the data source of an InfoPath form template.

If you bind multiple repeating tables that are located on the same View to the same nodes in the data source, InfoPath will display an icon indicating that the fields in the repeating tables are referring to the same data. This is not an error, it is just for informational purposes (see Figure 1).

Repeating table fields displaying duplicate data in InfoPath
Figure 1. Repeating table fields displaying duplicate data in InfoPath.

To display the same data in a repeating table on different places on an InfoPath form:

  1. From the Controls task pane, add a Repeating Table to your InfoPath form template.
  2. From the Data Source task pane, drag the repeating node of the repeating table you just added, drop it on the InfoPath form template, and select Repeating Table from the context menu that appears. Alternatively, you could create a second View, and repeat this step to add a repeating table that contains the same data as the repeating table of step 1 to your InfoPath form template

Your InfoPath form should now display the same data in multiple repeating tables on the form, and when you enter data in one repeating table, the same data will appear in the other repeating table on the InfoPath form.

Working with InfoPath