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.

ADVERTISEMENTS

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 =
  MainDataSource.CreateNavigator().SelectSingleNode(
  "//my:document", NamespaceManager).Value;


// Convert the base64 encoded string into a byte array
InfoPathAttachmentDecoder decoder =
  new InfoPathAttachmentDecoder(attachedFile);
byte[] attachment = decoder.DecodedAttachment;
string fileName = decoder.Filename;


// Add the file as an attachment to the SharePoint list item
item.Attachments.Add(fileName, attachment);

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.

 
 Subscribe via RSS or Email

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.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners
InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013
InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners
InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010
InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers
InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services


Related Posts

ADVERTISEMENTS

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services