How to submit an InfoPath form to a SharePoint list and attach the form to the list item

Learn how to use the Attachments collection of a SharePoint list item to save the XML of an entire InfoPath form as an attachment to a SharePoint list item.

ADVERTISEMENTS

In How to use the SharePoint object model to submit data from an InfoPath browser form to a SharePoint list I showed you how you can use the SharePoint Object Model to save the data from an InfoPath form as an item in a SharePoint list.

If you want to take this a bit further and also attach the entire InfoPath form to the SharePoint list item you created, you can add the following C# code just before the item.Update() line:

string formXml = MainDataSource.CreateNavigator().OuterXml;
byte[] attachment = Sytem.Text.Encoding.UTF8.GetBytes(formXml);
item.Attachments.Add("InfoPathForm.xml", attachment);

Now when you submit the InfoPath form to the SharePoint list, the XML of the entire InfoPath form will be stored as an attachment to the SharePoint list item.

Note:
You do not have to limit yourself to saving the entire InfoPath form as an attachment to the SharePoint list item. You could for example also extract data from fields on the InfoPath form, use this data to create a text file or even a Word document, and attach this text file or Word document as an attachment to the SharePoint list item. The possibilities are endless!

 
 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