InfoPath 2010 and SharePoint 2010 Integration using Custom WCF Service: Part 1 of 2






4.33/5 (2 votes)
Creating SharePoint List and Custom WCF application to read the SharePoint List information using Client Object model
- Part 1: Creating SharePoint List and Custom WCF application to read the SharePoint List information using Client Object model
- Part 2: Designing InfoPath Form to read the values from WCF Application. Publish and Deploy InfoPath form and setup site page to use InfoPath Web part to display the form
Introduction
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.
SharePoint Lists
So to start with, I have created a SharePoint List, named Projects. The fields are as below:
Title
- Single line of textClientName
- Single line of textPrimaryTechnology
- Single line of textSecondaryTechnology
- Single line of textDatabase
- Single line of textProjectManager
- Single line of textProjectType
- ChoiceProjectStartDate
- Date and TimeDuration
– NumberProjectEndDate
- Single line of textCreatedBy
- Person or GroupModifiedBy
- 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 asProjectManagementApplication
. Add a reference to theMicrosoft.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" Service="Tutorial.SP2010.ProjectManagementApplication.ProjectManagementService.PMService, $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 inProjectId
as parameter. - Now add class named
PMService
and implement theIPMService
. - 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.
<services> <service behaviorConfiguration="PMServiceBehaviour" name="Tutorial.SP2010.ProjectManagementApplication. ProjectManagementService.PMService"> <endpoint address="" name="PMServiceEndPoint" binding="basicHttpBinding" bindingConfiguration="BasicHttpEndpointBinding" contract="Tutorial.SP2010.ProjectManagementApplication. ProjectManagementService.IPMService"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" name="PMServiceEndPointMex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost/Design_Time_Addresses/ Tutorial.SP2010.ProjectManagementApplication. ProjectManagementService/PMService/" /> </baseAddresses> </host> </service> </services> <bindings> <basicHttpBinding> <binding name="BasicHttpEndpointBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" closeTimeout="03:00:00"> <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/> </binding> </basicHttpBinding> </bindings>
- 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 namedGetProjectDetailsById
. Before you start, you will need to add reference toSystem.ServiceModel.Activation
, and enableAspNetCompatibilityRequirements
. 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 theListItem
usingGetItemById
method ofSPList
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 theGetMethod
. Don’t forget to add dummy data to the list before you test using client.