When doing InfoPath form development I’m always doingcertain things on a consistent basis and one of them is building a .NET Classfrom my InfoPath form data source. Theapproach that I’m going to mention can be done in either version but theexample that I’m going to show is for InfoPath 2007. The reason for building a .NET Class from myInfoPath data source is because I’m usually using the form with a SharePoint2007 workflow (Visual Studio 2008) or .NET Web Service for submitting data to adatabase. The way I do it is very simpleand keeps the coding process from getting to complex. Key thing for me is to make things simple andeasy to maintain. Something to keep inmind is that we’re not building high transaction applications but InfoPath formsfor gathering information from people. In this article I’m going to show how to takean “Employee Status Report” form data source and convert it into a C#Class. If you’re a VB.NET person noworries because the process will be exactly the same except for one piece andI’ll point that out. One more itembefore we get started, when I’m working with InfoPath 2007 forms first then Ialways do is save my form to a project folder but unpackaged. To do this just use the “save as sourcefiles” command from the “File” menu.
The reason for doing this is to have the flexibility to getto any of the files that I want. Forthis instant I want access to the “myschema.xsd” and “manifest.xsf”. The “myschema.xsd” file is the XSD file forthe data source that is built with InfoPath designer and the “manifest.xsf”file is the main with all the instructions and makes InfoPath designer launch.
Now let’s take a quick look at the form for this example andthe design of the data source for the form.
As you can see the form is very simple but it’s reallyimportant to make sure that the data source is design logically and makes sincebecause we’re going to be using it as an object. After a quick glance I’m sure you noticedthat “MyFields” was renamed to “EmployeeStatusReport”. This was done to make sure it has a uniquename and could differentiate itself from other InfoPath form data sources. Everything else is just named descriptivelyto make sure it’s understandable from the code. I’m not saying that my name approach of the fields is the way to go andany approach would be acceptable as long as its descriptive.
Now that the form is built and saved to my project folderwere going to close InfoPath and open “Visual Studio 2008 Command Prompt”. The program is located on my machine at“Start -> Microsoft Visual Studio 2008 -> Visual Studio Tools”. Once the command window is open navigate outto the location of your InfoPath form files. It should look something like the below image.
The file that we are after is the “myschema.xsd” file. Now that we are in the location of the files let’srun the xsd.exe program to generatethe .NET Class. Below is the commandline statement that I use but there are many more options available and you canread all them by typing in xsd.exeand hitting enter.
xsd.exe /c /l:CSmyschema.xsd
Xsd.exe /c /l:VBmyschema.xsd
Once its done running (which should happen in matter of seconds)a message should appear saying that it wrote a new file called “myschema.cs”.
Once the file has been generated I just go rename the fileto the namespace name and then add it to my Visual Studio SharePoint workflowor .NET Web Service project. Once thefile is available in the project you have the capability to bind the datasource to the .NET Class and you have all your data is available in astandardized format. If you ever makechanges to your InfoPath data source you’ll need to rerun the xsd.exe commandagainst the new “schema.xsd” file and then just delete the old .NET Class fromthe Visual Studio project and add the new .NET Class to the project and you’redone.
I’ve talked with a lot of hard core developers in the pastabout this approach and a lot of them just look at me strange because I’mhaving a program build the code for me but to be honest it works and I don’tsee anything wrong with it for what I’m using it for. The cool thing is I never have to make anymodifications to that code file and it’s all generated by the xsd.exe program. For me this is a great way to get clients tostandardize there development process for building and supporting InfoPathforms.
After this weekend I’ll be putting together a couple morearticles on how I use the .NET Class with a .NET Web Service and Visual Studio2008 SharePoint Workflow project. When reading the next articles you’ll noticethat everything is just as simple as this one is. Simplicity is my main goal and notcomplexity.