通过编程方式保存InfoPath 2010表单数据到SharePoint列表
继续我们之前的博文,上一次我们创建了一个应用程序页并在页面中添加了xmlformview控件,在这里我们将讨论如何在宿主到xmlformview控件中(位于我们的自定义应用程序页)的infopath表单里通过OnSubmitToHost保存数据。
本文中我们将学习如何获取用户提交的infopath表单字段数据(这里是两个字段FirstName和LastName)并将整个表单以FirstName加LastName的命名方式保存到SharePoint库中。
首先,让我们修改xmlformview控件,添加OnSubmitToHost事件
<fv:XmlFormView ID=”formView” runat=”server” EditingStatus=”Editing” OnSubmitToHost=”FormView_SubmitToHost” Width=”700px”></fv:XmlFormView>
提交到宿主的代码如下:
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”, “http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-29T22:54:17″); doc.LoadXml(navigator.OuterXml); // 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; }
参考资料
Save infopath 2010 form data to SharePoint list programmatically