Thursday, September 5, 2013

How To Create a WCF Web Service in SharePoint 2013

Check out the whole SharePoint 2013 Solution Series
New Titles Added Weekly!

Many times you need to access SharePoint data from a web service. This could be for custom web parts, InfoPath Forms, BCS purposes, etc. When you need to access SharePoint data (e.g. list items, document information, etc.), the best method in SharePoint 2013 is to create a custom WCF web service that lives on your SharePoint farm.

Step 1 - Visual Studio 2012 Project
The first step is to open up Visual Studio 2012 in a SharePoint development environment and create a new project using the SharePoint Empty Project template.

Step 2 - Leverage the WCF Project Template
Your SharePoint project needs the WCF service components. It is easier to have them generated from a WCF project and copy them into your SharePoint WCF project. 

Step 3 - Copy the Service Classes to Your SharePoint WCF Project

Copy the IService1.cs and Service1.cs files that were generated in the WcfServiceLibrary project into your SharePoint WCF project. Once these files have been copied into your SharePoint WCF project, you may remove the WCF Service Library project from your solution.

Step 4 - Add References to Your SharePoint WCF Project
You need to add the System assembly references to your SharePoint WCF project. When you create an Empty SharePoint project, the Microsoft.SharePoint and  Microsoft.SharePoint.Client.ServerRuntime references are already in place. You no longer need to add these as you may have had done in previous versions of Visual Studio and/or SharePoint.

Step 5 - Define your Service and Data Contracts in the Service Interface
In your interface file (IService1.cs), remove the current operation contracts and define your own for your WCF service.
For example purposes, I needed to create a WCF Service for BCS and Search which requires a ReadItem method and a ReadList method. Therefore, in my ServiceContract, I created two OperationContracts - one for each.

The implementation of these methods will read list items in a SharePoint Calendar list and return the data within an object. In my example, the object is an EventItem which is a custom class. Therefore, in my DataContract, I needed to define the EventItem class.

Step 6 - Prep the Service Class
The service class (Service1.cs) implements the operations that you defined in the interface, however, there are some preparations that need to be performed first. This includes attributes and using statements.

Step 7 - Implement the Service Methods
In your Service class, add the implementation of the methods that you previously defined within your service interface.For my BCS example, I needed to implement a ReadItem and a ReadList method.

Step 8 - Create the SharePoint Service Registration File
The moving parts of the WCF service are now completed, however, in order for the service to be deployed and workable in SharePoint, the .svc file needs to be created. The .svc file contains the WCF service information as well as the referenced assembly (.dll) that is generated when you build the solution. The assembly information is added into the registration file upon the build and deployment of your SharePoint WCF Service.

Web services in SharePoint live in the ISAPI folder on each web-front-end and are referenced using  /_vti_bin/ within a SharePoint URL. Therefore, you must create an ISAPI folder in your project and then create the registration file within the ISAPI folder.
Next, you need to create the registration file within the ISAPI folder.
Step 9 - Activate Token Replacements for Service Files
The code that you pasted into the .svc file contains the assembly full name token ($SharePoint.Project.AssemblyFullName$). Visual Studio replaces this with the assembly from your project during build and deployment. However, you must instruct Visual Studio to perform this in .svc files. This requires a modification to the actual project file of your SharePoint WCF service.

Step 10 - Deploy Your SharePoint WCF Service
Right-click your solution and select Deploy. Visual Studio builds your solution, generates an assembly, updates the .svc file, creates a WSP file for deployment to stage/production, and deploys your SharePoint WCF service to your development SharePoint environment.

Step 11 - Test Your SharePoint WCF in SharePoint
Once your solution has been deployed, you may access the WCF service in SharePoint using the SharePoint root URL along with the path to the service:

http://<<sharepoint root>>/_vti_bin/Service.svc

To fully test the service you need to call it from a client application or use the svcutil.exe tool to invoke the service methods to insure data is being returned.

Using Your WCF Service in BCS
When creating an external content type using a SharePoint WCF Service, you need to enter and select the proper selections. This section shows examples of these settings:

Service Metadata URL

http://<<sharepoint root>/_vti_bin/Service.svc/mex?wsdl

Metadata Connection Mode


Service EndPoint URL

http://<<sharepoint root>>/_vti_bin/Service.svc

In just a handful of steps you can easily create a WCF Service in SharePoint that serves up SharePoint data. Using the WCF Service Library template as a guide and an empty SharePoint project, Visual Studio 2012 provides the tools and functionality to generate and deploy a SharePoint WCF service.

Get all of the details with supporting screenshots in this low priced Kindle guide:

Check out the whole SharePoint 2013 Solution Series
New Titles Added Weekly!

Thanks for your support!


No comments:

Post a Comment

Matched Content