Udostępnij za pośrednictwem


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;
 4: 
 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:     }
13: 
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.

Summary

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!

J

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

  • Anonymous
    March 11, 2007
    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?

  • Anonymous
    March 11, 2007
    Hi Rodrigo! Well you can create the mapping exactly the otherway round as well. So just create method like this: [WebMethod] 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: [WebMethod] public void SetEmployeeAsString(String employee) {    // TODO: handle incoming employee } I hope that this answered your question. Anyways... happy hacking! J

  • Anonymous
    March 30, 2007
    Thank you so much.  This was so helpful

  • Anonymous
    March 31, 2007
    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! J

  • Anonymous
    June 01, 2007
    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! v.

  • Anonymous
    June 01, 2007
    The comment has been removed

  • Anonymous
    June 01, 2007
    Thanks for the link. I will def try it :)

  • Anonymous
    July 04, 2007
    Thanks a lot!! It really works, solved me a big problem! :)

  • Anonymous
    August 02, 2007
    The comment has been removed

  • Anonymous
    September 03, 2007
    can we connect from infopath to SQL? thnx!

  • Anonymous
    September 13, 2007
    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. http://www.mmwebs.com/cgi-bin/contact.asp

  • Anonymous
    September 16, 2007
    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! J

  • Anonymous
    September 25, 2007
    Can you provide more details on the proxy Web service?  First time I have heard of that.

  • Anonymous
    September 26, 2007
    I posted awhile back about InfoPath and Web Service data connection and I got question about using own

  • Anonymous
    September 26, 2007
    Hi Robert! Since I couldn't give you simple answer I created a small post about my idea: http://blogs.msdn.com/jannemattila/archive/2007/09/26/adding-own-custom-web-service-to-sharepoint.aspx I hope it gives you starting point to continue. Anyways... Happy hacking! J

  • Anonymous
    October 25, 2007
    I have been asked to build this kind of example sooooo many times that now I really need to write this

  • Anonymous
    October 25, 2007
    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.

  • Anonymous
    October 25, 2007
    Hi Chaman! Checkout my new post: http://blogs.msdn.com/jannemattila/archive/2007/10/25/from-infopath-to-database-via-web-service.aspx I think that will answer your question... I certainly hope so because that was the reason why I wrote it :-) J

  • Anonymous
    December 07, 2007
    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. Thanks FCode

  • Anonymous
    December 19, 2007
    Excellent! Just what I needed. More tutorial like this please. Thank you very much

  • Anonymous
    March 06, 2008
    The comment has been removed

  • Anonymous
    March 19, 2008
    The comment has been removed

  • Anonymous
    April 23, 2008
    Hi, 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? Thanks

  • Anonymous
    August 07, 2008
    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. Thanks!

  • Anonymous
    February 13, 2009
    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. Thanks!

  • Anonymous
    February 16, 2009
    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 asp.net 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.

  • Anonymous
    May 14, 2009
    although today is 2009, but your article still useful. thanks a lot.

  • Anonymous
    November 15, 2009
    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.

  • Anonymous
    February 01, 2010
    Awesome post! Sarayu pretty much summed up my comment... ;)

  • Anonymous
    February 08, 2010
    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

  • Anonymous
    March 04, 2010
    Very conclusive post. Thanks JANNE, it helped save lot of time and brainstorm. Thanks a lot.

  • Anonymous
    September 21, 2010
    Janne, Thanks for this post. You did a great job putting this together!!!

  • Anonymous
    December 12, 2010
    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. Thanks Nomi

  • Anonymous
    February 12, 2012
    thanks a lot....It helped me very much..