Changing the Namespace for an InfoPath 2010 code-behind project

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.
Advertisements

7 Comments Add yours

  1. Matt says:

    Thanks! This helped me fix my issue opening the code behind from InfoPath!

    1. Chris says:

      You’re welcome! Glad it helped 🙂

  2. Kumar says:

    HI Chris,
    I have the same issue while opening my InfoPath form.(InfoPath has encountered an error. The operation failed).I trying to solve the issue from couple of days but I didn’t succeed .Please help me how can I achieve this issue. Let me know what information I provide to you from myself?

    1. Chris says:

      I’m sorry for replying to this so late. It got lost in my inbox.

      Does your form have code-behind? Have you saved it as files (as opposed to a .xsn file)?

  3. Etienne says:

    You’re a star my InfoPath friend 🙂
    Thanks

    1. Chris says:

      Thank you 🙂

      I’m glad it could help!

  4. JP says:

    Thanks, works for me!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s