Top 10 questions about InfoPath attachments
A compilation of the 10 most popular queries about the InfoPath attachment control, including using InfoPath attachments with SharePoint document libraries, SharePoint lists, and SharePoint workflows.
In the past months (and year), I've written much about how to use the InfoPath attachment - 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 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 attach 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:
- Add a Repeating Table with 1 column to your InfoPath form template.
- Go to the Data Source task pane and double-click the field under the repeating node for the Repeating Table.
- Change the Data type to Picture or File Attachment (base 64) and click OK.
- 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.
That is how you can attach multiple files 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:
- Programmatically restrict the size of an attachment in InfoPath
- InfoPath Video Tutorial: Programmatically restrict file types in InfoPath browser forms
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.
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
InfoPath 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 of InfoPath attachments and SharePoint, see:
- Submit an InfoPath attachment to a SharePoint Document Library
- Submit an InfoPath attachment as an attachment to a SharePoint List Item
- Submit an entire InfoPath form as an attachment to a SharePoint List Item
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:
- InfoPath Video Tutorial: Upload images from InfoPath to SharePoint using a custom SPD activty - Part 1
- InfoPath Video Tutorial: Upload images from InfoPath to SharePoint using a custom SPD activty - Part 2
7. Code to rename InfoPath File Attachment
To rename an InfoPath 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.
Recipe 100 of InfoPath 2010 Cookbook 3 (for C#) and InfoPath 2010 Cookbook 4 (for VB) teaches you how to create a custom SharePoint Designer workflow activity that can be called from a SharePoint Designer 2010 workflow to loop through attachments in an InfoPath form to be able to send an email with attachments via a workflow.
Recipe 98 of InfoPath 2010 Cookbook 3 (for C#) and InfoPath 2010 Cookbook 4 (for VB) teaches you how to upload a file that is stored in a file attachment control or an image that is stored in a picture control to a SharePoint document library or to a SharePoint picture library.
Recipe 94 of InfoPath 2010 Cookbook 3 (for C#) and InfoPath 2010 Cookbook 4 (for VB) teaches you how to download and add a file selected from a SharePoint document library as an attachment to a repeating table on an InfoPath form.
Recipe 55 of InfoPath 2010 Cookbook 5 teaches you how to write C# or VB code to retrieve the value of a cell in an Excel workbook that is stored in a file attachment control on an InfoPath form and display that value in a field on the form itself.
Recipe 54 of InfoPath 2010 Cookbook 5 teaches you how to write C# or VB code to create an Excel workbook from within an InfoPath form and store that Excel workbook in a file attachment control on the form itself.
Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author.