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

by S.Y.M. Wong-A-Ton

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.

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 the InfoPath form is submitted 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 (RSS | Email)

Related Posts

Comments to this post were closed 30 days after it was published.

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. Usage of 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.

Working with InfoPath