I've figured it out! Yay, I'm happy now! Thanks if anyone else attempted to solve the problem and I'll post my solution to hopefully help anyone else that may come across this in the future:
Summary Steps (InfoPath 2007 C# model)
-
Create a private string variable called 'initialInnerXml' and set it to ""
-
Create a 'Form Loading' event and within this set 'initalInnerXml' equal to 'myFields' innerXml
-
Create a private function/method called 'newRecord' that sets 'myFields' innerXml equal to 'initialInnerXml'
-
Call this function whenever you need to mimic the 'New Record' button functionality.
N.B. I've found that if you have multiple views and use the InfoPath 'New Record' button, whenever you click it, the view resets to the default view. Using this code solution, the view does not change.
Here's a code example:
public partial class FormCode
{
//This string records the initial myFields InnerXml to revert to
private string initialInnerXml = "";
//Call this whenever you want to mimic the 'New Record' InfoPath button
private void newRecord()
{
//Simply sets the myFields InnerXml back to what it originally was
MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields", NamespaceManager).InnerXml = initialInnerXml;
}
// NOTE: The following procedure is required by Microsoft InfoPath.
// It can be modified using Microsoft InfoPath. public void InternalStartup()
{
//Let InfoPath create these bits for you
EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
((ButtonEvent)EventManager.ControlEvents["Example_Button"]).Clicked += new ClickedEventHandler(Example_Button_Clicked);
}
//This is the Form Loading event. Let infopath create it for you
public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
//Simply records what myFields InnerXml is originally as the string initialInnerXml
initialInnerXml = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields", NamespaceManager).InnerXml;
}
//This is an example button on the form that creates a new Record
public void Example_Button_Clicked(object sender, ClickedEventArgs e)
{
//Call the function/method
newRecord();
}
}