Modifying InfoPath manifest.xsf file from script (1/5)

In this five part series we’ll show the reader how to programmatically manipulate InfoPath form templates.

Part 1 of 5: The story

There are many scenarios where you might need to modify an InfoPath form template (.XSN) after it’s already in use. Let’s say the URL of your Web Service changes.

With InfoPath 2003 you need to change the URL manually:

  1. Open the form in design-mode (e.g. Right-click on the XSN and select Design).
  2. Extract files of the form template (XSN) into the temporary directory using File | Extract Form Files… , then close InfoPath
  3. Find manifest.xsf in the directory and open it in Notepad (or some other text editor).
  4. Find and replace all instances of existing URL with the new one.
  5. Save the modified manifest.xsf.
  6. Right-click the modified manifest.xsf, select Design, and finally use File | Save As... to save the form template as a XSN file.

With InfoPath 2003 with Service Pack 1 (SP1), just open the form in Design mode, go to Tools menu, select Change Data Source, and in the wizard locate the new URL.

But this isn’t (just) a sneaky way to convince you to download the SP1 Preview.

What if you are responsible for maintaining hundreds of InfoPath forms which rely on the Web service? Even with SP1 this would be a lot of work. It would be nice to do it programmatically.

The good news is that this is possible, even without SP1. The InfoPath XSN format is really a CAB file, and the files that make up the template are XML files which can easily be modified programmatically. You can pack/unpack a CAB file with help of extract.exe and makecab.exe utilities that are accessible for Windows users. Now the only things you need is to implement a small utility using script or managed code that will do the described above sequence automatically. Inside of the utility you should do the following:

  1. Unpack XSN file using extract.exe utility to the local temporary directory.
  2. Find manifest.xsf file and open it as a plain XML file.
  3. Replace the corresponding URLs using the XML DOM.
  4. Save the modified manifest.xsf.
  5. Pack all files into the XSN using makecab.exe utility.
  6. Clean up your temporary directory.

If you write a wrapper around this process to enumerate all the form templates, your hundreds of forms will be updated just in a few minutes.

Using this approach you can change not only URLs but SQL connection strings, files names, etc. If you want to learn more about the XSN format and the files that make it up, check out the InfoPath 2003 SDK. As always, be careful – save backup copies, test your code, test the templates before and after, etc.

At the end of this discussion, the patient reader expects a good sample that supports the idea explained above. And we will definitely do it in the following parts 2-5 of the series.

To be continued.