How to submit a file attached to an InfoPath form as an attachment to a SharePoint list item
Learn how you can programmatically save a file attachment from an InfoPath form as an attachment to a SharePoint list item when the InfoPath form is submitted to the SharePoint list.
I previously wrote about how you can attach an entire InfoPath form to a SharePoint list item when you submit an InfoPath form to a SharePoint list.
As you may know, you can use a File Attachment control to attach a document to an InfoPath form.
If you want to save a file, which you have stored in a File Attachment control on an InfoPath form, as an attachment to a SharePoint list item, you can use the same technique I discussed in my previous blog post.
To do this, you must first decode the file attachment in the InfoPath form, that is, convert the base64 encoded string of the attachment in the InfoPath form into a byte array, by using the InfoPathAttachmentDecoder class discussed in the encoding decoding article How to encode and decode a file attachment programmatically by using Visual C# in InfoPath.
Then you can use the following code to save the byte array of the file as an attachment to a SharePoint list item:
// Retrieve the base64 encoded string of the attached file
string attachedFile =
// Convert the base64 encoded string into a byte array
InfoPathAttachmentDecoder decoder =
byte attachment = decoder.DecodedAttachment;
string fileName = decoder.Filename;
// Add the file as an attachment to the SharePoint list item
where my:document is the XML node in the InfoPath form that represents a File Attachment control.
You must place this code before the item.Update() line.
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 of S.Y.M. Wong-A-Ton. 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, S.Y.M. Wong-A-Ton.