SharePoint Pals
 | Sign In
Step by Step Procedures to create a WCF Service Application for SharePoint 2013
by Sathish Nadarajan 19 Jun 2013
Author
SharePoint MVP
Visits   
Today  :   16     Total  :    43395

This article describes about the creation of a WCF Service and deploying it into the SharePoint 2013 environment. WCF Services plays a major role in SharePoint 2013 Apps based development model by acting as a bridge between SharePoint Client Object Modal and SharePoint Client Object Modal

Since Server Side Object model is not available for SharePoint App , any operation or business requirement in a SharePoint App has to be performed with Client Side Object Model (Microsoft.SharePoint.Client Namespace) only . But this namespace may not be enough to cater all the requirements which we expect in our Provider Hosted Application. At that time, what we can do is, we can create some WCF Service and Deploy them on the SharePoint Farm. From our Provider Hosted Application, we can invoke this service as REST calls. By doing so, we are violating the concept of App Development Model. But in certain requirements, there is no other way. Say for example, the SocialRatingManager. There is no CSOM equivalent class for SocialRatingManager class.

Let us see the step by step procedure to create the WCF Service in SharePoint 2013

1. Open Visual Studio as Administrator

2. Click New Project.

clip_image002

3. Select Empty SharePoint 2013 Solution.

clip_image004

4. Enter the URL of a Site Collection and select Farm Solution.

clip_image006

5. The solution will looks like this.

clip_image008

6. Add the following References on the Add Reference.

a. Microsoft.Office.Server.UserProfiles

b. Microsoft.SharePoint

c. Microsoft.SharePoint.Clieint.ServerRuntime

d. System.ServiceModel

e. System.ServiceModel.Web

f. System.DirectoryServices

g. System.Configuration

7. Hence the solution will looks like

clip_image010

8. Add Layouts folder to the solution.

clip_image012

9. Make the Appropriate folder structure as shown in figure.

clip_image013

10. Since we cannot add a svc file directly, add a text file and rename it to .SVC

clip_image015

11. Open the SVC file and place the following code on it.

 <%@ServiceHost   language= "C#"   Factory= "Microsoft.SharePoint.Client.Services.MultipleBaseAddressDataServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"   Service= "SharePoint.WCFService.Sample.Code.SampleService" %>

12. Create a folder called ‘Code’ and create 2 files. ISampleService.cs and SampleService.cs

13. Open the ISampleService.cs and add the following using Tags and Modify the Class as below.

 using System.ServiceModel;  using System.ServiceModel.Web;    [ServiceContract]  public interface ISampleService  {   [OperationContract]   [WebInvoke(Method = "GET",   ResponseFormat = WebMessageFormat.Xml,   BodyStyle = WebMessageBodyStyle.Wrapped,   UriTemplate = "SampleServiceCall({SampleValue})")]   string SampleServiceCall(string SampleValue);  }

14. Now, we have our interface ready. Now open the SampleService.cs file which is going to implement the Interface described above.

15. Modify the class as shown below.

  using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;  using System.Threading.Tasks;  using System.ServiceModel;  using System.ServiceModel.Web;  using System.ServiceModel.Activation;    using Microsoft.Office.Server.Social;  using Microsoft.Office.Server.UserProfiles;  using Microsoft.Office.Server.Microfeed;  using Microsoft.SharePoint;  using Microsoft.Office.Server.ReputationModel;  using Microsoft.Office.Server.SocialData;    using System.Configuration;    using System.Security.Principal;  using System.Web;  using Microsoft.Web.Hosting.Administration;    namespace SharePoint.WCFService.Sample.Code  {   [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]   public sealed class SampleService : ISampleService   {     public string SampleServiceCall(string SampleValue)   {   return "Success";   }     }  }

16. Select the Project and go to Properties. Modify the Deployment Target Property as WebApplication.

clip_image017

17. Modify the SiteURL to the webAppliaction url appropriately.

18. Deploy the solution and test from the Browser by giving the following url.

https://MyServer/Sites/SiteCollection/_layouts/15/SharePoint.WCFService.Sample/Services/SampleService.svc/SampleServiceCall(test)

19. We will get the response as below.

  <?xml version="1.0"?>  <SampleServiceCallResponse xmlns="http://tempuri.org/">   <SampleServiceCallResult>Success</SampleServiceCallResult>  </SampleServiceCallResponse>

With this simple steps, we are able to create our Service and deploy them on our SharePoint farm. The service application may be very simple. But from the Provider Hosted Application, if you are not able to achieve any functionality, then immediately think on the Service Applications. Happy Coding.

Download SourceCode

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation