Add data from Infopath 2010 Form to Sharepoint List on Submit button click

Here you will learn how to save the data in the infopath form hosted inside the xmlformview control ( on our cutsom applictaion page) on OnSubmitToHost of the xmlformview.

In this post you will learn how to retrieve user submitted data in infopath form fields (fields are FirstName and LastName) and how to save the whole form in SharePoint library with the name created using FirstName and LastName fields.
Lets first modify the xmlformviewcontrol – Adding the OnSubmitToHost

Code for submit to host

protected void FormView_SubmitToHost(object sender, SubmitToHostEventArgs e)
SPWeb web = SPContext.Current.Web;

web.AllowUnsafeUpdates = true;

// Load the XML and save it as a byte array
System.Xml.XPath.XPathNavigator navigator = formView.XmlForm.MainDataSource.CreateNavigator();

Byte[] formBytes = System.Text.Encoding.UTF8.GetBytes(navigator.OuterXml);
// Create XmlDocument from the form XML

XmlDocument doc = new XmlDocument();

XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);
nsm.AddNamespace(“my”, “;

// Load name information from the XML

XmlNode nodeLastName = doc.SelectSingleNode(“/my:myform/my:UserInformation/my:LastName”, nsm);
string name = (nodeLastName != null) ? nodeLastName.InnerText : string.Empty;

XmlNode nodeFirstName = doc.SelectSingleNode(“/my:RoomBooking/my:UserInformation/my:FirstName”, nsm);
string firstname = (nodeFirstName != null) ?nodeFirstName.InnerText : string.Empty;

// Generate file name
string filename = String.Format(“{0}{1}_{2}.xml”, name, firstname, DateTime.Now.ToString(“yyyyMMdd”));

// Open library and save XML
SPFolder formLibrary = web.GetFolder(LIBRARY_NAME);
formLibrary.Files.Add(filename, formBytes);
web.AllowUnsafeUpdates = false;


Post a Comment

Popular Posts