InfoPath and Web Service data connection

I have used InfoPath mainly with SharePoint and my experiences have been quite good. However I have always felt like something was missing... something that would really warm my developer heart. And I have found it! It's nice integration with Web Services. InfoPath can retrieve and submit data to the Web Services with just few lines of code for Web Service and few button clicks at the InfoPath. But if you're creating serious application you should definitely create XML Schema that is shared with applications. It would be easy to use ready-made schema at the InfoPath and in your other applications as well. I'm just going to create small example that uses web services to get data to few fields at the InfoPath. In my experience it's fairly common that you have a lot of fields at the InfoPath and then you would like to autopopulate some of the fields based on some other fields. You probably want to get your employee/customer/product data from some of the back-end systems like ERP, CRM, etc. So this example demonstrates simple way of achieving that kind of functionality. I'm going to present this development process in "real world scenario". So it means that developer creates web service and UI Designer creates the InfoPath form (Warning! It may contain sarcasm!). But enough talking... Let's start hacking!

1. Create web service to return employee data

First we're going to create simple web service that returns some employee data (and now 'we' stands for person in developer role):

 1: using System;
 2: using System.Web;
 3: using System.Web.Services;
 5: public class Service : System.Web.Services.WebService {
 6:     public struct Employee {
 7:         public int EmployeeNumber;
 8:         public String Title;
 9:         public String FirstName;
10:         public String LastName;
11:         public Decimal Salary;
12:     }
14:     [WebMethod]
15:     public Employee GetEmployee(int employeeNumber) {
16:         Employee employee = new Employee();
17:         employee.EmployeeNumber = employeeNumber;
18:         employee.Title = "Developer";
19:         employee.FirstName = "John";
20:         employee.LastName = "Doe";
21:         employee.Salary = 12345;
22:         return employee;
23:     }
24: }

Of course your data should be retrieved from your favorite back-end system, but I'll just return some dummy data. Okay now we just compile (and deploy if necessary) our web service so it can be used by InfoPath. And copy the WSDL url because you need it later when dealing with the InfoPath form.

2. Create InfoPath form

If I would be really lazy I would use Design a form template -> Web Service since it's really straightforward if you want to retrieve and submit data with Web Services. But I'm just going to take Design a form template -> Blank. Because in many cases the UI designer doesn't know much about programming. Designer just drag & drops the controls to the template and stops working with the template when it looks good (Note: this form is my handwriting so it hasn't even seen any UI design experience :-).

After that designer gives the template to the developer. And developer sees right away that designer didn't think about those pretty important things like schemas and data sources

Developer doesn't want to do any extra work so he just renames the controls so that they're are easily understandable.

After that developer changes the Form Template Properties at the File -> Properties. And now developer is ready to add web service datasource to the InfoPath form.

3. Create Web Service datasource

 Under Tools -> Data Connections add new data connection:

Select Receive data and press Next.

Select Web service and press Next.
Paste the url of the WSDL and press Next.

Select GetEmployee method from the list (it's the only one so easy selection 🙂 and press Next.

Take away selection from Automatically retrieve data when form is opened and press Finish.

4. Create Rule to the button that uses the new datasource

Now we have managed to create connection to our web service. Let's add the Rule to the button so that web service is called when user clicks the button. The Rule will contain following phases:

  1. Set parameter for the Web Service Call
  2. Make the Web Service Call
  3. Set field values for each field from the secondary datasource (=Web Service)

Right click on the button and select Properties. Click on the Rules button and then click Add button from the Rules dialog. Write name of this new rule (like Get employee data from Web Service).

Then we need to add some actions to the rule. Just click Add action button:
Verify that your checkbox is Set a field's value and then press button next to the Field textbox. Then you can select the employeeNumber from the GetEmployee data source.

After that you do the same for the Value field. That was phase 1.

Phase 2 is really simple... just add new action for Query using a data connection: GetEmployee.

Phase 3 contains quite many button clicks... just create Set a field's value actions for each value return from the Web Service field:

Finally we have following Actions in our Rule:

And now we're ready to test our form. Just press Preview toolbar item. When form opens you can fill Number field with some number and populate the other fields by pressing the Get employee data button:
Fields are filled with the retrieved data from the Web Service.


Now you know howto create simple Web Service call from InfoPath. But you may wonder that how did those field names come from the web service struct into the InfoPath form? Well if you look at the services WSDL you can see, that it contains definitions for the Employee data structure:

So it's really easy to create small struct that contain the necessary datas you want to expose from your service. It's easy and fast, but more correct way would be using the schemas. See Creating a web service for InfoPath from in 25 easy steps article from the links below. And of course posting the data to the web service would be exactly the same as this example. Just create new Rule that first copies all values from the fields in the form to the datasource fields. And after that you just call the correct method from the datasource. Isn't it easy... I think it is.

That was my InfoPath post this time. And maybe next time something completely different stuff 🙂

Anyways... Happy hacking!


Links to similar resources

Build a Custom SharePoint Web Service for Your InfoPath 2003 Documents 
-- Howto retrieve and submit data with XmlDocument

Creating a web service for an InfoPath form in 25 easy steps 
-- Creates InfoPath form and then extracts XML Schema out of it (Note: Exporting source files in InfoPath 2007: File->Save as Source Files). Then xsd.exe is used to create .cs -file. It's then added to Visual Studio and used when creating the web service. And developer can enjoy full support from IntelliSense.

Querying and Updating a Database Using Web Services in InfoPath and ASP.NET 
-- Example that uses DataSets with InfoPath.

Comments (34)

Cancel reply

  1. Rodrigo says:

    Thanks a lot for this post. Quite useful. I’m attending  bootcamp MOSS2007 in short. Got an example for sending a form (xml) right from InfoPath to a web service?

  2. Hi Rodrigo!

    Well you can create the mapping exactly the otherway round as well. So just create method like this:


    public void SetEmployee(Employee employee)


       // TODO: handle incoming employee


    And then set field values from InfoPath and then submit it to the web service.

    Or if you really want to send xml form as a string you can do that too. Just add Submit data datasource with parameter "Entire form" and "Submit data as string". With those settings you should be fine. And of course to your web service something like this:


    public void SetEmployeeAsString(String employee)


       // TODO: handle incoming employee


    I hope that this answered your question.

    Anyways… happy hacking!


  3. John says:

    Thank you so much.  This was so helpful

  4. Hi John!

    I’m glad you like… I hope I can create some posts in the future that will be helpful as well.

    Anyways… happy hacking!


  5. floppylo says:

    Hi Janne,

    I want to create a web service to insert infopath data into a SQL db. Is there any VB example rather than C you can point me to?

    Thanks in advance!


  6. Hi floppylo!

    I don’t have VB example since I’m not so good at it 🙂 But I think that there are some websites that actually transform code from C# to VB. Just search for it "convert c# to VB".. I found this one but I haven’t tested it:

    Anyways… happy hacking!


  7. floppylo says:

    Thanks for the link. I will def try it 🙂

  8. Alexandra says:

    Thanks a lot!! It really works, solved me a big problem! 🙂

  9. Nuntius says:

    Okay, first of all thanks, this is the best explanation I have found of how to do this.

    Second, I am just wondering if InfoPath is supposed to be user friendly enough for the average office user, why the heck isn’t a web service creation tool not built into InfoPath?

    Also, why can’t I just connect directly to the db? I get pretty frustrated with it since I can write a simple form pretty quickly in php or asp/AJAX without having to go through all the hassle. So can someone explain, "Why the complexity?"

    I built a nice form in InfoPath and then tried to submit it and it gave me all of these compatibility issues with Sharepoint. I am trying to give it a chance but so far it just sucks!

  10. anne says:

    can we connect from infopath to SQL?


  11. <moojjoo> says:

    Great Blog Post.  I am in the process of needing to do some SQL Server Reporting Services (SSRS) and the current forms post to SharePoint.  Trying to read that data using WebServices from SharePoint is a nightmare.  I am going to attempt to post the InfoPath using WebSevices to a SQL Backend so the reporting will be really easy to develop.

    If you have any suggestions please contact me.

  12. Hi moojjoo!

    Did you mean that you tried to use SharePoint web services (can’t remember but was it List.asmx) to get your data? I’m justing thinking out loud, but if you think that’s too difficult (or something) you can always create proxy web service to actually handle all the internal data gathering logic from SharePoint.. if you wish. That would be easy to do.

    Anyways… Happy hacking!


  13. Can you provide more details on the proxy Web service?  First time I have heard of that.

  14. I posted awhile back about InfoPath and Web Service data connection and I got question about using own

  15. Hi Robert!

    Since I couldn’t give you simple answer I created a small post about my idea:

    I hope it gives you starting point to continue.

    Anyways… Happy hacking!


  16. I have been asked to build this kind of example sooooo many times that now I really need to write this

  17. Chaman says:

    Hi, I am trying to submit the form to a webservice from InfoPath2007 form. In Webservice I am accepting data as an object. I am facing an issue like the all data are comming as null. But when I tried to did it with InfoPath2003 I worked.

    I am wondering if there is any serialization issue or is there any other problem.

    Can anyone please help me out with this.

  18. Hi Chaman!

    Checkout my new post:

    I think that will answer your question… I certainly hope so because that was the reason why I wrote it 🙂


  19. Fcode says:

    Hi Mattila,

    Thanks for nice blog. Its really helpfult but you forgot to mention about the Trust level of the form. So

    The above example will not work until and unless you go to Tools-> Form Options-> Security and Trust and select Full Trust or Domain Trust level.



  20. Arun Arya says:

    Excellent! Just what I needed. More tutorial like this please. Thank you very much

  21. Nilesh B says:

    will you explain hos to get data from database, insteaed of Hard coded value.

  22. JOSETELLAN says:

    Hi Mattila, this post is brilliant to understand how to connect sharepoint, webservices and infopath. great  great job!!!

    But i´ve a question for you. doing all steps you describe i have a successfully retrieve data from my web service by pressing previus view button but when i publish my form template in a sharepoint site, infopath says there is a problem connecting to data source and in the same dialog box also says somo rules could´t be applied.

    i don´t understand why the process run ok with previus view and not with sharepoint?

    any sugestions please?

    many thanks for doing the post. congrats.

  23. P says:


    I am new to Infopath and web services… but I tried following ur steps and got lost around the point of getting WSDL url…

    Can you explain how to reach that point?


  24. Ryan says:

    Not sure if you’re still responding to posts on this, but… I tried to follow this and another tutorial on writing a web service to receive an InfoPath submission.  It seems to work, but when the form is submitted, all the fields within the web service are null.  No data comes across.  If I submit the entire XML document as a string and set up the web service to receive a string, I can see that the data is included in the submission.  But it doesn’t come across when I have an object (the class from Visual Studio’s xsd.exe).  I think this may be the same issue Chaman had (and I reviewed your newer post to see if that fixed it.  Do you know why the fields would all be null?  I tried: adding the web service location to my trusted sites; modifying IE settings to allow cross-domain data; and publishing the form with Full trust.


  25. AmberCF says:

    This was such a great example!  I’m mostly a VB programmer and I’ve never written a Web service until today.  You made it so easy, not to mention the integration with InfoPath.


  26. Tito says:

    Thanks to the information, its very useful your tutorial, but I’ve a problem with my SharePoint enviroment, and I think If you could help me. I have a webService in other machine that I have SharePoint, and when I publish my infopath form the data conection between infopath and the other webservice machine it is imposible, the error is "Unknown Server". And If I try to conect with this webservice, in the same server with application, without sharepoint It will connect succesfully. What Should I change in my sharepoint to conect my Infopath to a external webservice?, thanks a lot.

  27. JamesLiang says:

    although today is 2009, but your article still useful.

    thanks a lot.

  28. Sarayu says:

    Amazing!!! Thank you very very very very very veryyyyyy much . I am quite new to webservices and was reading the tutorial. I am required to do the same job, u demonstrated above. Love u.

  29. Nayden says:

    Awesome post! Sarayu pretty much summed up my comment… 😉

  30. kesava.b says:

    Hi Janne, The post is really awesome. But I have one problem, when I open the form in the browser it is throwing error like "An error occurred accessing a data source." Please help me out

  31. avikal says:

    Very conclusive post. Thanks JANNE, it helped save lot of time and brainstorm. Thanks a lot.

  32. Dave says:

    Janne, Thanks for this post. You did a great job putting this together!!!

  33. Nomi says:

    I am having a problem with webservice. I have made a custom webservice and called it from infopath 2007 using the above tutorial. It works in IE 6 but it does not work in IE8. It gives "resouce can not be access error". When I see IIS logs, error no is 401.2. Any Idea whats wrong? Anonymous access is enabled on my service.



  34. Rutika says:

    thanks a lot….It helped me very much..

Skip to main content