In this tutorial, we will be looking at designing an InfoPath form. We will use a custom WCF service to retrieve data from SharePoint List and display it on the InfoPath Form. The same service will be used to save the data to the SharePoint List. Part 1 of this tutorial will focus on creating a SharePoint list and developing a WCF service to read the data from the SharePoint List.
So to start with, I have created a SharePoint List, named Projects. The fields are as below:
Title - Single line of text
ClientName - Single line of text
PrimaryTechnology - Single line of text
SecondaryTechnology - Single line of text
Database - Single line of text
ProjectManager - Single line of text
ProjectType - Choice
ProjectStartDate - Date and Time
Duration – Number
ProjectEndDate - Single line of text
CreatedBy - Person or Group
ModifiedBy - Person or Group
WCF Implementation – Read Data
Now, we need a WCF service which will read the data from SharePoint List. We will be passing ID from the InfoPath form to the service, which will be used to fetch the data from list using Object model. This is pretty straight forward, if you are a SharePoint developer.
- In Visual Studio 2010, create an Empty SharePoint Project; I have named it as
ProjectManagementService. It is a good idea to have a different name for the Solution file. I have named it as
ProjectManagementApplication. Add a reference to the
Microsoft.SharePoint.Client.ServerRuntime assembly. This assembly will be required as it contains the Service Host Factory classes.
- Right-click on the Project and add the SharePoint Mapped Folder, as shown below:
- This opens a pop-up, select ISAPI and click OK. This adds ISAPI mapped folder in the project. This is where the service will be deployed. It is a good practice to deploy the services in their independent folders. Add a folder inside the ISAPI folder, where the SVC file will reside. I have created a folder named PM Service for my svc file.
- To add the service file (.svc), right click on the newly added folder, and add a new text file. Rename the file and add extension as .svc. This file is hosted in IIS and contains the details of assembly information and code behind file.
<%@ ServiceHost Language="C#" Debug="true"
$SharePoint.Project.AssemblyFullName$" CodeBehind="PMService.cs" %>
- We will need an Interface for Service Contract and a class that implements the service contract. Add 2 folders in the project. I have named my folders as ServiceContract and Service.
- First add an interface in ServiceContract Folder. I have added a file named IPMService.cs. We need a Data Contract that will hold the project related information. Then we add the Operation contract for getting and setting the project related information. Setter method will take in
string (the entire XML content from InfoPath) as a parameter. The getter method will take in
ProjectId as parameter.
- Now add class named
PMService and implement the
- Add a Config file to the service folder created in ISAPI folder, name it Web.config. This file will contain the binding and endpoints related information. You can use the below configuration settings for bindings and endpoints.
<endpoint address="" name="PMServiceEndPoint"
<dns value="localhost" />
<endpoint address="mex" name="PMServiceEndPointMex"
binding="mexHttpBinding" contract="IMetadataExchange" />
- We are done with initial task of setting up the service. Below is how the solution looks in solution explorer after all the files are added. You can also use CKS Development Tool which installs a Visual Studio template to create the WCF Service for SharePoint.
- In the Service Contract, I have 2 operation contracts (methods exposed using the WCF), one for getting the data from SharePoint List and one for setting the data in SharePoint List. The
get method is named
GetProjectDetailsById. Before you start, you will need to add reference to
System.ServiceModel.Activation, and enable
AspNetCompatibilityRequirements. Do this by adding the below attribute to the service class.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
- Implement the
GetProjectDetailsById method. I have used SharePoint Object model to read the
GetItemById method of
SPList object. The item id will be passed from the InfoPath form as a parameter.
- Deploy the WCF service. We don’t need to have InfoPath form to test the WCF Service. Visual Studio provides us with a client application to test the WCF service. Start Visual Studio Command Prompt and execute “
WCFTestClient” command to run WCF client application. Add the service URL and pass the parameter to the
GetMethod. Don’t forget to add dummy data to the list before you test using client.