Pieter van der Westhuizen

Creating Microsoft InfoPath add-ins in Visual Studio with Add-in Express

Microsoft InfoPath makes it relatively easy for end-users to design XML-based forms. It is also a stress-free way for users to design forms that can connect to databases, XML schemas or even to SharePoint Lists.

Earlier this year, Microsoft has also announced that it will discontinue InfoPath to pursue a more integrated experience for Office forms. So look out for some InfoPath features being integrated into Word, Access and SharePoint in the future!

Although InfoPath is at the end of its life, it doesn’t mean people will stop using it right away, so there will still be room for writing an add-in or two. We all know when it comes to writing add-ins for any product in the Microsoft Office suite that Add-in Express is the toolset for the job.

Microsoft InfoPath base objects

Let’s have a closer look at each of main InfoPath objects one by one.

Application object

As with other Microsoft Office applications, the object that acts as the starting point for most add-ins is the Application object. In InfoPath’s case it contains three Application objects:

  • _Application
  • _Application2
  • _Application3

The first two Application objects have been superseded by the _Application3 object and merely remain in the object model for backward compatibility. For any new InfoPath add-ins, use the _Application3 object. Add-in Express automatically takes care of this for you by adding a property to the AddinModule class that references the correct Application object.

public InfoPath._Application3 InfoPathApp
{
    get
    {
        return (HostApplication as InfoPath._Application3);
    }
}

XDocument object

The XDocument object is an important object in the InfoPath object model as it can be used to programmatically interact and manipulate the XML source of the InfoPath document. You can access the XDocument object by referencing the XDocument property of the ActiveWindow object. The ActiveWindow object is a property on the Application3 object.

To iterate through all the fields in an InfoPath Form, you’ll have to use the DOM property of the active XDocument object. An InfoPath form is essentially an XML document, so looping through its fields involves looping through the child nodes of the forms’ document element.

Looping through the InfoPath form fields

Use the following code to loop through the form fields:

private void loopFieldsRibbonButton_OnClick(object sender,
    IRibbonControl control, bool pressed)
{
    InfoPath.Window activeWindow = null;
    InfoPath.XDocument activeDocument = null;
    IXMLDOMDocument domDocument = null;
    IXMLDOMElement docElement = null;
    IXMLDOMNodeList childNodeList = null;
    string nodeNames = "";
 
    try
    {
        activeWindow = InfoPathApp.ActiveWindow;
        if (activeWindow.WindowType == InfoPath.XdWindowType.xdEditorWindow)
        {
            activeDocument = activeWindow.XDocument;
            if (activeDocument != null)
            {
                domDocument = activeDocument.DOM;
                if (domDocument != null)
                {
                    docElement = domDocument.documentElement;
                    childNodeList = docElement.childNodes;
 
                    for (int i = 0; i < childNodeList.length; i++)
                    {
                        IXMLDOMNode childNode = childNodeList[i];
                        if (childNode.nodeTypeString == "element")
                            nodeNames += childNode.baseName + " : " +
                                childNode.nodeTypedValue.ToString() +
                                Environment.NewLine;
                    }
                    MessageBox.Show(nodeNames);
                }
            }
        }
    }
    finally
    {
        if (childNodeList != null) Marshal.ReleaseComObject(childNodeList);
        if (docElement != null) Marshal.ReleaseComObject(docElement);
        if (domDocument != null) Marshal.ReleaseComObject(domDocument);
        if (activeDocument != null) Marshal.ReleaseComObject(activeDocument);
        if (activeWindow != null) Marshal.ReleaseComObject(activeWindow);
    }

Setting InfoPath form field values

Setting the form field values, is done in a similar fashion as you would set element values in an XML document. You can get a reference to a specific field’s node by using the getElementsByTagName method of the IXMLDOMDocument object. In this example, we’re using the Travel request template and setting the purpose and name fields.

private void setFieldsRibbonButton_OnClick(object sender,
    IRibbonControl control, bool pressed)
{
    InfoPath.Window activeWindow = null;
    InfoPath.XDocument activeDocument = null;
    IXMLDOMDocument domDocument = null;
    IXMLDOMNodeList purposeNodes = null;
    IXMLDOMNode purposeNode = null;
    IXMLDOMNodeList nameNodes = null;
    IXMLDOMNode nameNode = null;
 
    try
    {
        activeWindow = InfoPathApp.ActiveWindow;
        if (activeWindow.WindowType == InfoPath.XdWindowType.xdEditorWindow)
        {
            activeDocument = activeWindow.XDocument;
            if (activeDocument != null)
            {
                domDocument = activeDocument.DOM;
                if (domDocument != null)
                {
                    purposeNodes = domDocument.getElementsByTagName("my:purpose");
                    purposeNode = purposeNodes[0];
                    if (purposeNode != null)
                        purposeNode.text = "Client Visit";
 
                    nameNodes = domDocument.getElementsByTagName("my:name");
                    nameNode = nameNodes[0];
                    if (nameNode != null)
                        nameNode.text = "Pieter van der Westhuizen";
                }
            }
        }
    }
    finally
    {
        if (nameNode != null) Marshal.ReleaseComObject(nameNode);
        if (nameNodes != null) Marshal.ReleaseComObject(nameNodes);
        if (purposeNode != null) Marshal.ReleaseComObject(purposeNode);
        if (purposeNodes != null) Marshal.ReleaseComObject(purposeNodes);
        if (domDocument != null) Marshal.ReleaseComObject(domDocument);
        if (activeDocument != null) Marshal.ReleaseComObject(activeDocument);
        if (activeWindow != null) Marshal.ReleaseComObject(activeWindow);
    }
}

Customizing the InfoPath Ribbon UI

Like all of the applications in the Microsoft Office 2013 suite, InfoPath 2013 also supports the Office Ribbon UI. To add your own ribbon tab to the InfoPath window, you first need to add a new ribbon tab control (ADXRibbonTab) to the AddinModule designer surface by clicking on its corresponding icon on the AddinModule designer toolbar.

Adding a new ribbon tab control to the AddinModule designer surface

Add-in Express supports a rich visual designer for designing and laying out the ribbon UI’s elements. When selecting the newly added ADXRibbonTab the visual designer will be shown inside Visual Studio. The following image illustrates a simple ribbon tab with a custom group and two buttons:

Designing a custom ribbon tab with a custom group and two buttons in Visual Studio

Creating a custom InfoPath context menu

Add-in Express also makes it easy to add your own items to the built-in context-menus of InfoPath by providing an ADXRibbonContextMenu component.

Adding a new context menu component to the AddinModule designer surface

After adding this component to the AddinModule designer surface, you can design the context menu items using the built-in visual designer.

Designing a custom context menu for InfoPath using Add-in Express visual designer

You can specify the right-click menu to which you want to add the items to by setting the ADXRibbonContextMenu’s ContextMenuNames property. This will display a dialog with which you can choose the name of the built-in context menu. In the following image, we’ll add a button to the context-menu when the user right-clicks a textbox in InfoPath:

The dialog where you can choose the name of the built-in context menu

You’ll also need to specify the ADXRibbonContextMenu’s Ribbons property. By setting it to InforPathFiller, your custom context menu will only be visible when the user is completing the form, not whilst the form is being designed.

Creating a custom InfoPath task pane

Office Task panes are a convenient way for users to use different commands and to perform various tasks. It also provides developers with a way to display more information and show a larger UI than they are able to do with the ribbon UI.

MS InfoPath also supports custom task panes and Add-in Express similarly enables developers to quickly and easily create their own task panes. To create your custom InfoPath task pane, first add a new Windows Forms User Control to your project.

Adding a new Windows Forms User Control to your project

You can design the user control as you would a standard windows form. After you’ve designed the task pane, select the AddinModule designer surface and click on the ellipses (…) button next to its TaskPane property.

Click on the ellipses button next to the TaskPane property to display the ADXTaskPane Collection Editor dialog window.

This will display the ADXTaskPane Collection Editor dialog window. Here you can set the various properties for the custom task pane. The most import properties are:

  • ControlProgID – this is the name of the user control to use as the task pane;
  • DockPosition – the location of the task pane, can either be right, left, top, bottom or floating; and
  • SupportedApps – One task pane can be used in multiple Office applications. To only use it in InfoPath set this property to InfoPath.

The ADXTaskPane Collection Editor dialog window

Customize the InfoPath Backstage view

Lastly, we’ll take a look at how you can add your own items to the File menu or rather the backstage view of InfoPath. In order to add your own items, you first need to add an ADXBackstageView component to the AddinModule designer surface.

Adding a new BackstageView component to the AddinModule designer surface

Add-in Express provides a powerful visual designer for the Office backstage view, with which you can design a large variety of different layouts.

Designing a custom Backstage view in Visual Studio

Thank you for reading. Until next time, keep coding!

Available downloads:

This sample Outlook add-in was developed using Add-in Express for Office and .net:

Sample InfoPath add-in (C#)

You may also be interested in:

Post a comment

Have any questions? Ask us right now!