Creating a web service for an InfoPath form in 25 easy steps

It took me a while to figure out how to do the InfoPath/webservice thing starting with an InfoPath form. Maybe this is blindingly obvious to everyone but me, but here it is anyway:

1) Create an InfoPath form from a new, blank form:

 

2) Lay out your form, using sections, grouping, and naming controls as appropriate:

 

3) Extract the form files (File | Extract Form Files) to a location you can find later.

 

4) Open Visual Studio .Net (I'm using 2003 in this example; this will work with 2005 as well. Remember - we're only building the web service in VS.Net; not InfoPath code)

5) Create a new ASP.Net web service project.

6) Switch to the code view of Service1.asmx and note the namespace. (Rename Service1 or add a new Web Service if you choose to)

 

7) Open the Visual Studio command prompt:

           Start | All Programs | Microsoft Visual Studio .Net 2003 | Visual Studio .Net Tools | Visual Studio .Net 2003 Command Prompt

8) You'll have to either close InfoPath or copy the myschema.xsd to another location (the file will be locked by InfoPath in design mode)

9) Go to the location you specified in step 8. Run xsd against the myschema.xsd in that directory; specify the namespace to the namespace from step 6:

           xsd.exe myschema.xsd /classes /l:cs /n:InfoPathFormService

 

10) You'll now have a myschema.cs file. Add that to the web service project (right-click on the project, Add -> Existing item)

11) Define a new webservice method:

[WebMethod]

public void SubmitForm(myFields newForm)

 

12) myFields is the root of the object model for your form:

13) Put any logic in the WebMethod you need to parse the values out of the form, perform business logic, update databases, etc.

14) Finally, let's hook the InfoPath form to the submit webservice. Publish the web service to a web server:

 

15) Copy the URL of the web service.

16) In the design mode of our InfoPath form, open Tools | Submitting forms:

 

17) Enable submit commands and buttons, select "Web Service" for "Submit to"

18) Add a data connection, paste the URL of your web service in the location box:

 

19) Next, then select the operation for your submit form action, then Next > again:

 

20) The next dialog will prompt to match the form's submission to the web method's parameter. Select "Field or group" then click the node button to the right of the text box:

 

21) Select the myFields node, then click "OK"

22) Select to Include the XML Subtree, including selected element

23) Next, Finish, then OK

24) Publish your form

25) Now, when users fill out the form, they can save it to their local drive as an XML file, or submit it to your web service.