Click here to Skip to main content
15,881,173 members
Articles / Productivity Apps and Services / Sharepoint

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

Rate me:
Please Sign up or sign in to vote.
4.33/5 (2 votes)
7 Oct 2013CPOL4 min read 27.7K   407   7   3
Creating SharePoint List and Custom WCF application to read the SharePoint List information using Client Object model

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:

  1. Title - Single line of text
  2. ClientName - Single line of text
  3. PrimaryTechnology - Single line of text
  4. SecondaryTechnology - Single line of text
  5. Database - Single line of text
  6. ProjectManager - Single line of text
  7. ProjectType - Choice
  8. ProjectStartDate - Date and Time
  9. Duration – Number
  10. ProjectEndDate - Single line of text
  11. CreatedBy - Person or Group
  12. 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.

  1. 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.
  2. Right-click on the Project and add the SharePoint Mapped Folder, as shown below:

    Image 1

  3. 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.

    Image 2

  4. 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.
    ASP.NET
    <%@ ServiceHost Language="C#" Debug="true"
    	Service="Tutorial.SP2010.ProjectManagementApplication.ProjectManagementService.PMService,
    	$SharePoint.Project.AssemblyFullName$" CodeBehind="PMService.cs" %>
  5. 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.
  6. 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.
  7. Now add class named PMService and implement the IPMService.
  8. 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.
    XML
    <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>
  9. 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.

    Image 3

  10. 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.
    C#
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
  11. Implement the GetProjectDetailsById method. I have used SharePoint Object model to read the ListItem using GetItemById method of SPList object. The item id will be passed from the InfoPath form as a parameter.
  12. 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Consultant
India India
I have been designing and developing systems in .NET since 2006. Started out with .NET 1.1, and progressed through to current version. Started working with SharePoint 2010 in 2011, and am currently gaining experience in SharePoint Online and hosted apps.

Comments and Discussions

 
QuestionInfoPath 2007 Pin
Member 1132103018-Dec-14 6:11
Member 1132103018-Dec-14 6:11 
QuestionThere's a deployment error Pin
shielo21-Jun-14 15:27
shielo21-Jun-14 15:27 
AnswerRe: There's a deployment error Pin
Rupesh Tarwade21-Jun-14 20:02
professionalRupesh Tarwade21-Jun-14 20:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.