I have a form that takes in some input parameters, passed from an prior process. These are the only fields that need to be populated. I would like once the input parameters are set in the form to Submit and Close the form with no user input.
I found a way to get around the inability to have an execute command initiated from Form Loading event, by having the form loading event edit a field and based on the change of that field it call the submit process.
The form submits fine, but errors when I try to close the form.
using Microsoft.Office.InfoPath;
using System;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using mshtml;
namespace JSR_Issue_Tracking
{
public partial class FormCode
{
public void InternalStartup()
{
EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
((ButtonEvent)EventManager.ControlEvents["Submit"]).Clicked += new ClickedEventHandler(Submit_Clicked);
EventManager.XmlEvents["/my:myFields/my:xmlFilename"].Changed += new XmlChangedEventHandler(xmlFilename_Changed);
}
public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
XPathNavigator xn = this.MainDataSource.CreateNavigator();
if (String.IsNullOrEmpty(xn.SelectSingleNode("/my:myFields/my:jsrID", NamespaceManager).Value))
{
try
{
string _jsrID = e.InputParameters["jsrid"];
string _jobName = e.InputParameters["jobname"];
string _titleCode = e.InputParameters["titlecode"];
string _psr = e.InputParameters["psr"];
xn.SelectSingleNode("/my:myFields/my:jsrID", NamespaceManager).SetValue(_jsrID);
xn.SelectSingleNode("/my:myFields/my:jobName", NamespaceManager).SetValue(_jobName);
xn.SelectSingleNode("/my:myFields/my:titleCode", NamespaceManager).SetValue(_titleCode);
xn.SelectSingleNode("/my:myFields/my:PSR", NamespaceManager).SetValue(_psr);
Set_XML_Filename();
}
catch (Exception ex)
{
MessageBox.Show("An error occured while trying to import data into the form.\nPlease manually fill in the top row and click submit.",
"Error on Import", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
}
}
private void Set_XML_Filename()
{
XPathNavigator xn = this.MainDataSource.CreateNavigator();
string xname = xn.SelectSingleNode("/my:myFields/my:xmlFilename", NamespaceManager).Value;
if (String.IsNullOrEmpty(xname))
{
xname = Convert.ToString(long.Parse(System.DateTime.Now.ToString("yyyyMMddHHmmssfff")), 16).ToUpper();
xn.SelectSingleNode("/my:myFields/my:xmlFilename", NamespaceManager).SetValue(xname);
}
}
public void Submit_Clicked(object sender, ClickedEventArgs e)
{
Submit_Form();
}
private void Submit_Form()
{
FileSubmitConnection submit = (FileSubmitConnection)(DataConnections["SharePoint Library Submit"]);
try
{
submit.Execute();
MessageBox.Show("Form Submitted.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
Application.ActiveWindow.Close(true);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public void xmlFilename_Changed(object sender, XmlEventArgs e)
{
Submit_Form();
}
}
}