Showing posts with label Infopath 2010. Show all posts
Showing posts with label Infopath 2010. Show all posts
Monday, December 05, 2011
Infopath 2010 form drop-down list change event get data programmatically
Infopath 2010 form drop-down list change event get data programmatically
A short code snippet to get the data from a datasource on change event of a drop-down list in Infopath 2010 form.
In the code below the method InternalStartup() is called to register the event change for the dropdown and the method mydropdown_Changed() is called to access the actual datasource “MyDATASOURCE” for retrieving the values for Field1 or sharepoint Title column from the sharepoint list datasource(MyDATASOURCE).
The parameter passed to the Datasource via the webservice is ID of the drop-down item.
using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;
using System;
using System.Xml;
using System.Xml.XPath;
namespace Getdatanamespace
{
public partial class FormCode
{
public void InternalStartup()
{
EventManager.XmlEvents["/my:myform/my:table1/my:mydropdown"].Changed += new XmlChangedEventHandler(mydropdown_Changed);
}
{
public partial class FormCode
{
public void InternalStartup()
{
EventManager.XmlEvents["/my:myform/my:table1/my:mydropdown"].Changed += new XmlChangedEventHandler(mydropdown_Changed);
}
public void mydropdown_Changed(object sender, XmlEventArgs e)
{
{
XPathNavigator form = MainDataSource.CreateNavigator();
// Set parameter for web service request and query connection
DataSources["MyDATASOURCE"].CreateNavigator().SelectSingleNode(“/dfs:myFields/dfs:queryFields/q:SharePointListItem_RW/q:ID”,NamespaceManager).SetValue(e.NewValue); ->> sending the ID as a parameter to the webservice call which will access the datasource
DataSources["MyDATASOURCE"].CreateNavigator().SelectSingleNode(“/dfs:myFields/dfs:queryFields/q:SharePointListItem_RW/q:ID”,NamespaceManager).SetValue(e.NewValue); ->> sending the ID as a parameter to the webservice call which will access the datasource
DataSources["MyDATASOURCE"].QueryConnection.Execute();
// Create navigator on web service response
XPathNavigator resultnav = DataSources["MyDATASOURCE"].CreateNavigator();
XPathNavigator resultnav = DataSources["MyDATASOURCE"].CreateNavigator();
// Set fields with values from web service response
XPathNavigator Field1 = form.SelectSingleNode(“/my:myform/my:table1/my:field1″, NamespaceManager);
Field1.SetValue(resultnav.SelectSingleNode(“/dfs:myFields/dfs:dataFields/d:SharePointListItem_RW/d:Title”,NamespaceManager).Value);
}
XPathNavigator Field1 = form.SelectSingleNode(“/my:myform/my:table1/my:field1″, NamespaceManager);
Field1.SetValue(resultnav.SelectSingleNode(“/dfs:myFields/dfs:dataFields/d:SharePointListItem_RW/d:Title”,NamespaceManager).Value);
}
}
}
}
Save infopath 2010 form data to Sharepoint list programmatically
Save infopath 2010 form data to Sharepoint list programmatically
In continuation with my previous post where I created an application page and added the xmlformview control to the page, in this post i will show you 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
<fv:XmlFormView ID=”formView” runat=”server” EditingStatus=”Editing” OnSubmitToHost=”FormView_SubmitToHost” Width=”700px”></fv:XmlFormView>
Code for submit to host
protected void FormView_SubmitToHost(object sender, SubmitToHostEventArgs e)
{
{
SPWeb web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;
// Load the XML and save it as a byte array
System.Xml.XPath.XPathNavigator navigator = formView.XmlForm.MainDataSource.CreateNavigator();
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();
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);
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);
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;
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”));
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;
}
SPFolder formLibrary = web.GetFolder(LIBRARY_NAME);
formLibrary.Files.Add(filename, formBytes);
web.AllowUnsafeUpdates = false;
}
Subscribe to:
Comments (Atom)