After reading this post by the InfoPath blog team on adding an InfoPath project to TFS source control, I went about organizing my form so that I could dump it in source control.

Since the form will eventually incorporate scores of elegant code, I added VSTA code-behind using the handy new Developer ribbon.  I quickly changed the namespace of my code to match our TFS naming standard, saved the project, closed the code, and re-opened it (Alt+Shift+F12) to make sure that everything stuck.  It didn’t:

FUUUUUUUUUUUUUUUUUUUUUUUUUUUU

InfoPath has encountered an error.  The operation failed.  Thank you for the descriptive awesomeness.

I closed the project, snooped around the directory containing the code, and found the %ProjectName%.Designer.xml file.  This contains the following entry:

hostitem:namespace=”%ProjectName%”

where %ProjectName% is the name of my InfoPath project.  I just changed this to the namespace that I assigned to the class library, re-opened the project, and boom, it worked.

To summarize, if you want to change the namespace for an InfoPath code-behind project, do the following:

  1. Change the namespace declared in the code (e.g. namespace Company.Project.Forms.ThisFormName)
  2. Change the namespace specified for the class library by right-clicking the class library, selecting Properties, then specifying the default namespace in the Application tab.
  3. Try to build the project– you will get an error complaining about the namespace.  Double-clicking the error opens the %ProjectName%.Designer.cs file; scroll to the top of this class and modify the namespace to match the namespace of the form code class.
  4. Save and close the code as well as the form (make sure it is published as source files, NOT a .xsn file– for more info, see the InfoPath blog team post that I linked to above).
  5. Navigate to the source code directory and open the %ProjectName%.Designer.xml file.  Find this entry: hostitem:namespace=”%ProjectName%” and change %ProjectName% to the namespace you specified above.
  6. Save, close, and re-open your form.  You should now be able to open the code-behind project.