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:
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:
- Change the namespace declared in the code (e.g. namespace Company.Project.Forms.ThisFormName)
- 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.
- 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.
- 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).
- 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.
- Save, close, and re-open your form. You should now be able to open the code-behind project.
