February 6, 2012
Developer In-Depth:
prev
next
- Business logic
- governance
- application development
- BizTalk
- SharePoint
- database
- jQuery
- SOA
- programming
- Visual C#
- Visual Studio
- Exchange
- documents
- PHP
- services
- Microsoft Office
- customization
- Exchange server
- security
- collaboration
- .NET
- SharePoint 2010
- CA
- CodePlex
- developer
- search
- document management
- portal
- WSS
- Web development
- Web sites
- authentication
- XML
- Microsoft
- software
- policy
- MOSS
- Web Parts
- Office
- Silverlight
- tools
- sandbox
- SharePoint Service Account
- InfoPath
- ASP.NET
- Windows
- server
- architecture
- Libraries
- SharePoint administration
Connect an InfoPath Form to a WCF Service Running On SharePoint
In this article, we are going to build on the solution we created last week, where we designed and deployed an InfoPath 2010 form for collecting customer information. We are going to develop and deploy a custom WCF service to SharePoint and use it as a secondary data source for our InfoPath form.The WCF Service
So, without further ado, lets get right into the dirty stuff and fire up Visual Studio 2010. Once the environment starts up, choose to create an Empty SharePoint Project and give it a nice, intuitive name. Since we are going to create a service we can use to retrieve customer information from the Adventure Works database, we will call it AdventureWorksCRMService.
On the next screen, we will be sure to choose to deploy our solution as a farm solution and click the Finish button.
We should now have a pretty robust solution that Visual Studio 2010 has been kind enough to create for us. It contains on-board capabilities for defining features and automatically packaging and deploying the solution as a WSP.
The next thing we need to do is bring in a reference to the Microsoft.SharePoint.Client.ServerRuntime assembly. This assembly contains the Service Host Factory classes that we will use later. Do not select Microsoft.SharePoint.Client.Runtime from the .NET tab in the Add Reference dialog. You need to browse to the Global Assembly Cache (GAC) at C:\Windows\assembly\ to find it.
We also need to create a SharePoint Mapped Folder to the ISAPI folder of our SharePoint implementation. To do this, right-click the project name, then select Add, then SharePoint Mapped Folder.
Select the ISAPI folder.
Since its always a good idea to segment your custom code from the standard SharePoint files, lets create a subfolder called AdventureWorksCRM to keep things separate. In that folder, create a new text file to hold the code for your service (name it Customer.svc) and a configuration file (naming it web.config is fine).
Now, we are going to add three class files as placeholders. One of them is going to house our data contract, which we can use to instantiatie/return a Customer object, one will house our service contract (interface) and the other one will hold our main service implementation. Your Solution Explorer should now look something like this:
Now we can start coding up our solution. Lets start with our Customer class. Add a Reference to System.Runtime.Serialization. You can find this one on the .NET tab of the Add Reference dialog.
For this class, we are going to merely create several properties (DataMember objects) that will make up our Customer object. Based on the information contained in the AdventureWorksLT database that is part of the standard Microsoft sample database installation. Here is what the code should look like:
Now we need to implement our Service Contract. In this code, we are going to create an OperationContract object that is designed to receive a parameter of the Customer ID and return the other information about the customer in the Customer object.
We are going to need another Reference here, to System.ServiceModel. This code is super simple, and it looks like this:
Is that simple enough for you?
So, the last class that we need to code is our service implementation. As you would expect, we are going to implement our interface from the last step, make a simple call out to SQL Server and return a Customer object. Here is what that code looks like:
Networking Solutions

Discover how to start developing for the Android platform with this extensive guide, which provides a reference to the Android platform as well as a look at developing your first Android application. You'll explore the top 10 features for developers as well as learn design and development tips that go beyond the phone and target tablet development as well.