InfoPath - reading template.xsd in code for type checking

InfoPath is the world's most advanced XML-based form system.  Each InfoPath document is a fully structured XML file, and the template contains the XSD schema definition for the XML file.  This includes a bunch of information such as min/max occurrences, as well as the type information for each of the elements.

Because the information for the element's type is stored separately in the xsd file, it isn't possible at runtime to workout what the type of each element is supposed to be. 
When we are given:

  • my:value1

If we can't read the template.xsd file, we don't know if that's a number, a string, a datetime nor do we know if the field is nillable.  Some fields in InfoPath, such as the boolean and the datetime fields, can't be blank.  They have to be set to nil.

 

Here's how you can read the template.xsd in code

An InfoPath Form

 

Here is an InfoPath form with a few fields

image

 

The XML File Connection

 

Add a XML File Data Connection to an existing file in the template, I use "template.xml"

image

Save the template, and now drop into the code behind.

 

Code

public void InternalStartup()
{
    EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
}

public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
    FileQueryConnection file = this.DataConnections["template"] as FileQueryConnection;
    file.FileLocation = "template.xsd";
    file.Execute();
    XPathNavigator template = this.DataSources["template"].CreateNavigator();
    string notes = "";

    foreach (XPathNavigator nav in this.CreateNavigator().Select("//*", this.NamespaceManager))
    {
        string localname = nav.LocalName;

        XPathNavigator element = template.SelectSingleNode("//*[@name='" + localname + "']", template);
        if (element != null)
        {
            notes += string.Format("{0} is of type {1} {2}; ", localname, element.GetAttribute("type", ""), element.GetAttribute("nillable", ""));
        }
    }

    this.CreateNavigator().SelectSingleNode("/my:myFields/my:notes", this.NamespaceManager).SetValue(notes);
}

  • The interesting part is at the top, where we bait and switch the FileQueryConnection to read the template.xsd schema file instead.  Because it is a valid XML file, InfoPath will read it happily.
  • We can then map any field in the main datasource to the corresponding definition in the template, and pull out the additional metadata such as type or nillable.

 

Result

image

 

Download

Making InfoPath 2010 Preview/Debug work again

Quick blog.  How I fixed InfoPath 2010 Debug/Preview error, after installing Office 2013.

InfoPath cannot open the selected form because of an error in the form's code.
InfoPath will fail to load this form because Microsoft .NET Framework 3.5 is not installed or is installed incorrectly

Install .NET Framework 3.5

First, if you are on Windows 8, you do need to install the .NET Framework 3.5 feature.  Do this via Add Windows Feature control panel.

 

Reverse the .NET Assembly Binding Redirect Policies

Fix these two policies that was redirecting v14 references to v15.

  • C:\Windows\assembly\GAC_MSIL\Policy.14.0.Microsoft.Office.InfoPath.Client.Internal.Host\15.0.0.0__71e9bce111e9429c\ Policy.14.0.Microsoft.Office.InfoPath.Client.Internal.Host.config
  • C:\Windows\assembly\GAC_MSIL\Policy.14.0.Microsoft.Office.InfoPath\15.0.0.0__71e9bce111e9429c\ Policy.14.0.Microsoft.Office.InfoPath.config

If you just change it to bind to the original version 14:

  • <bindingRedirect oldVersion="14.0.0.0" newVersion="14.0.0.0"></bindingRedirect>

 

Missing IPDMCTRL

If you see complains about ipdmctrl, copy it from:

  • C:\Program Files (x86)\Microsoft Visual Studio 11.0\Visual Studio Tools for Office\PIA\Common\IPDMCTRL.dll to C:\Program Files\Microsoft Office\Office14\

You can find IPDMCTRL in any Visual Studio Tools for Office installation.  Your directory could be different.

 

Use the Fusion Log Viewer tool

I figured this out using the fuslogvw tool - which logs .NET assembly loading events, and so I could figure out what it was trying to load when debugging, and failing.

image

 

Pics or it didn't happen

image

 

Here is both VS2012 debugging an InfoPath 2013 form, at the same time as VSTO debugging a separate InfoPath 2010 form.

InfoPath - Form stuck on Installing, Upgrading or Deleting

When you publish an InfoPath Form through Central Administration, a few things happen:

  1. Central Administration creates a WSP package for this form.
  2. Central Administration creates timer jobs to deploy the package to each of the web applications that uses this form (defined by Activate to Site Collection, in Central Administration)
  3. Administration Service on each server then runs the timer job to deploy the form.
  4. The deployment job actually does a bunch of things:
    1. Creates a list of upgrade templates in \14\TEMPLATE\FEATURES\FT-01-72d7f7bc-0c6e-ba52-fa91-bb24c0014ed6
      Note, for some sites, this list is incredibly long, and can take a long time. I can't think of any good reason why we need all the versions, but may be this can potentially affect form upgrades.
    2. Creates a content type for the form
    3. Copy the form template to %site collection%/Form Templates/template.xsn
    4. (do other stuff)

Review this article from Microsoft: http://blogs.msdn.com/b/infopath/archive/2006/10/23/behind-the-scenes-of-administrator-approved-form-templates.aspx 

So when a form is stuck on "Installing" or "Upgrading"

 

  1. Check timer jobs. If there are jobs that can't start - they will wait
    1. Check all administration service are running on both servers
    2. Check timer service is running (timer service calls administration service to deploy)
  2. If your administration service is OFF, then you can try using the command line to execute those timer jobs
    1. stsadm -o execadmsvcjobs
    2. The benefit is if there's exceptions thrown here you'd see it in the console
    3. Do this on all SharePoint servers. If the administration service is running, then this stsadm command won't do anything.
  3. If your job is missing, you can check in Solution Management /_admin/Solutions.aspx
    1. Go to Solution Management under Central Administration /_admin/Solutions.aspx
    2. Find the farm solutions that for the form. The name would be form-formname.wsp.
    3. If the solution isn't deployed, you can select deploy - global
    4. If your admin service isn't running, you'll get a warning saying that jobs are scheduled but no admin service means they won't run.
    5. Use stsadm -o execadmsvcjobs
  4. If nothing else works, or if we want to do a complete removal.
    1. In form templates, select the form template, and hit Remove.

Form is stuck on "Deleting"

  1. Go to Solution Management under Central Administration /_admin/Solutions.aspx
  2. Find the farm solutions that for the form. The name would be form-formname.wsp.
  3. If the status is Deployed, then retraction hasn't started.
  4. Click the package and select Retract Solution.
  5. If your admin service isn't running, you'll get a warning saying that job is scheduled but no admin service means they won't run.
  6. In command line, stsadm -o execadmsvcjobs, on both servers.
    C:\ > stsadm -o execadmsvcjobs
    Executing job-application-server-admin-service.
    Executing job-password-management.
    Executing solution-deployment-form-tradepackageform.wsp-0.
    Operation completed successfully.
  7. You'll see this back on solutions.
  8. Name:    form-packageform.wsp
    Type:    Core Solution
    Contains Web Application Resource:    No
    Contains Global Assembly:    No
    Contains Code Access Security Policy:    No
    Deployment Server Type:    Front-end Web server
    Deployment Status:    Deployed
    Deployed To:    Globally deployed.
    Last Operation Result:    The solution was successfully retracted.
    Last Operation Details:   
    SRV02 : The solution was successfully retracted.
    SRV03 : The solution was successfully retracted.
    Last Operation Time:    1/8/2013 11:01 AM

  9. You can then remove the solution package.
  10. If you go back to Form Template management, the form will be removed from the list, and you can re-upload as usual.

InfoPath form load rules and loading event order

 

A strange gotcha in InfoPath concerning which event runs first.

  • Create an InfoPath form, with a field.
  • Create a Form Load rule, that sets this field to 1.
  • Add code behind, and create a Form_Loading event, that sets the same field to 2.

 

In InfoPath rich client form / debugging

  • The result number is 2

In InfoPath browser form

  • The result number is 1

 

Keep this order in mind if you use both Form Load rules as well as Form_Loading event.

InfoPath 2013 uses new Online Pictures instead of Clip Art

 

The InfoPath 2013 Preview doesn't really have many new features.  But this one little one made me smile.

image

In InfoPath 2013, the Clip Art ribbon is replaced with Online Pictures.

image

For those of you wondering what was there before, in InfoPath 2010 it looked like this.  Yes it's that little button that we never used.

This dialog appears when you click it.

image

 

Searching for a victim's face...

image

 

image

 

A few things would make this even more awesome:

  • Add my commonly used Clip Arts to my skydrive, and link it up.  This probably works already, except my Office 2013 Preview isn't hooked up to my normal Microsoft Account.
  • Allow additional websites, I find iconfinder.com to be extremely good.
  • Or allow URL to be used directly in the dialog.