Table of Contents
Hello World Example
Exercise - Moving Items Between ListBoxes
Exercise - Repeating Sections handled Manually
Changed Events
Developing with Visual Studio 2010
Hello World Example
I will start by opening up InfoPath 2010 and allowing for custom development. First go to File –> Options –> More Options –> Design and choose C#.
Then create a new blank form and immediately save it as Form.xsn.
Now I’ll add three controls to my InfoPath form, a text box, a calculated value with a blank calculation and a button. The text box will automatically bind to a field named field1. Create a new text field named field2 and bind it to the calculated value.
Now open up the buttons properties window and click Edit Form Code. This will open the form’s Visual Studio Tools for Applications (VSTA) project for editing.
Note that you may have to install VSTA by going to Add / Remove programs and right clicking on Microsoft Office (Or InfoPath) and choosing “Add / Remove Features
Here is what you should see when the VSTA project opens.
In keeping with the spirit of Hello World examples, I’m not going to explain anything about what you’re looking at. Replace the text // Write your code here. with the following code:
public void CTRL3_5_Clicked(object sender, ClickedEventArgs e)
Here is what it should look like inside of VSTA.
Now press F5 to run your form in debug mode with your new custom code. Enter a value inside the textbox and see your value returned to you inside the calculated value.
This simple example could have been accomplished without any code, but it opens the door to endless opportunities. The pattern of querying for data using xpath, iterating through the results with an XPathNodeIterator, navigating around parent, child and sibling nodes with an XPathNavigator and setting the Value or InnerXml properties of xml nodes is basically all you will be doing with custom InfoPath code.
The important thing to realize when doing custom InfoPath development is that you are writing code to manipulate an xml document. After your xml manipulation code runs, InfoPath will read the xml you’ve manipulated and render a user interface on top of it.
Moving Items Between ListBoxes
Here is an example I’d like to see someone accomplish without custom code. Take a look at the form below. I have a list on the left named ThingsToChooseList and a list on the right named ThingsChosenList. In between them I have a MoveRight button and a MoveLeft button. Under the ThingsChosenList I have two more buttons, a MoveUpButton and MoveDownButton. I am also going to use the form loading event to load up some items into the ThingsToChooseList.
Notice inside the button properties you have the ability to name the buttons. Choosing readable names instead of CTRL#_# will make things a lot easier on you and anyone else that might have to read your code.
Here is what VSTA should look like before you start writing any code
Here is the code to make it all work:
And the functionality should look like this
Let’s look at the recurring themes inside the code.
this.MainDataSource – The this property refers to the form itself, which has a MainDataSource property. The MainDataSource is the data source that you design when you drop controls into the form.
XPathNavigator – Variables of type XPathNavigator are the real workhorses for xml manipulation. They can execute xpath, add and delete child xml elements. They can also navigate around an xml document, moving between parent nodes and child nodes, as well as between sibling nodes. They can also add, edit and remove xml elements or attributes.
XPathNodeIterator – When you execute an xpath expression the results go into a XPathNodeIterator variable. XPathNodeIterator has the count of items found and a MoveNext() method that loads each items into the Current property. The Current property is an XPathNavigator.
The code to drive moving the items left and right and up and down is basically identical to the Hello World example. I make queries using xpath, I iterate through the results using the XPathNodeIterator, and I navigate around the document using the XPathNavigator setting values where appropriate.
Repeating Sections handled Manually
Here’s one piece of functionality I needed almost immediately when using InfoPath for a client. InfoPath allows some menus for adding and deleting sections in a repeating section, but you can’t show or hide the menus based off of logic with out of the box InfoPath. I’ll show you how using code.
Create a repeating section inside an InfoPath form and bind it to a repeating group called RepeatingSection inside a group called RepeatingSections. Add a button named AddNewSectionButton above the repeating section and a button inside the repeating section named DeleteThisSectionButton.
Here is the code to make it work.
And the functionality should look like this
Now you have control over adding and deleting sections.
Changed Events
Some events like button events and the form loading event are straightforward but the changed events can get tricky. The trick with the changed events is that they bubble up through the xml document. For example, if you created a changed event at the root of your main data source, any change to the document would fire an event. Because events can come from any child node, you’ll probably want to examine the sender variable and the e.Operation variable inside your event handler to make sure you are responding to the correct event.
lets add a changed event to our RepeatingSections element. I will also add a group named EventsFromRepeatingSections with a repeating text field named EventFromRepeatingSections to display some information about what is happening inside of our changed event handler.
And now I will just click around my repeating sections and we can see the events bubbling up to my RepeatingSections element.
Developing with Visual Studio 2010
The visual studio that comes with InfoPath is called VSTA and is much different than the Visual Studio developers around the world adore. VSTA is at least 5 years old and allows for only .NET 2.0 development. It doesn’t support some of the most amazing .NET features such as LINQ. Now I will show you a little hack that will allow you to use .NET 3.5 and Visual Studio 2010 (VS2010) for your InfoPath development.
First you will need Visual Studio 2010. There is a free express edition that should suffice but if you are doing real development beg or borrow your way to a higher edition.
Create a new windows class library inside of VS2010 by going to File->New->Project->Windows Class Library. Make sure to give your class library the same name as your form!
Next we need to copy some references from VSTA into our VS201o project. The references we need to move over are Microsoft.Office.Infopath, Microsoft.VisualStudio.Tools.Applications.Adapter, Microsoft.VisualStudio.Tools.Applications.Contract and System.AddIn.Contract. Simply copy the paths of from the properties window of your VSTA project when you click on the reference, and paste the path into the Browse tab of the add reference window inside of VS2010.
Here are the paths to the references on my computer
C:\Program Files\Microsoft Office\Office14\InfoPathOM\InfoPathOMFormServices\InfoPathOMFormServicesV12\Microsoft.Office.Infopath.dll
C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.Tools.Applications.Adapter.dll
C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Tools.Applications.Contract\8.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Tools.Applications.Contract.dll
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\System.AddIn.Contract.dll
Next open up VSTA and right click on your project in the solution explorer. In the Build Events section add the following commands. Your paths will be different but you should get the idea
"C:\Windows\Microsoft.NET\Framework64\v3.5\MSBuild.exe" "c:\users\administrator\documents\visual studio 2010\Projects\Form\Form\Form.csproj"
xcopy /y "C:\Users\administrator\Documents\visual studio 2010\Projects\Form\Form\bin\Debug\Form.dll" "$(TargetDir)"
Now copy the files FormCode.cs, FormCode.designer.cs and InfoPath.snk into the root of your VS2010 project.
Finally, right click on your project inside the VS2010 solution explorer, selecting signing, checking New, and selecting InfoPath.snk.
And your done!
One downside I’ve noticed is that debugging still needs to occur inside VSTA. This is actually a plus if you only have VS2010 express, which cannot attach to processes for debugging. But if you have a higher edition the debugging experience in VS2010 is much better than VSTA. If anyone can show me how to debug inside VS2010 I’d appreciate it!












































Now I will re-enable Kerberos authentication and everything works fine. 















