SharePoint 2013 Create and Host Web Service

In this post we will discuss about how we can create WCF/Web service in SharePoint environment. Here you can get step by step instruction to create web service. This web service can be used to get data from share point. This service can return output in json and xml format. this service can be used in client side code as well as server side code. Suppose that there is a requirement say you want to perform same action from multiple platforms or multiple applications, So in such scenarios you can go with service. Its too simple to create and implementation is same as what we are doing in C# code. In WCF services hosting is always a challenge. But in SharePoint web service we can host on existing web application. So there is no need to put extra efforts to host SharePoint web service. 

Below is the step to step instruction to create SharePoint Web Service.

1. Open visual studio with Run as administrator

2. Create new solution and select SharePoint 2013 - Empty Project

 3. Provide url where you want to deploy this service.
4. Then solution will look like.

5. Add mapped layout folder to solution.

6. Add new folder under Layouts >> IExchangeSP13Sevice and name it as Services.

7. Folder structure should look like below.

8. Go to newly created folder services and add new item.

9. Select text file from template and name it as IExchangeService.svc.

10. Click on project root and add new folder and name it as Code.

11. Folder structure will look like below.

12. Go to newly created folder "Code" and add new class.

13. Put class name as ServiceMethods.cs

14.  Newly created class will look like.

15.  Add one more class at same location.

16. Put class name as IServiceMethods.cs. This class will be used as an interface. All service contracts will be declare here.

17.  Newly created class will look like.

18. Add below mentioned references.

19. Change Assembly Deployment Target of project to WebApplication.

20. Below are the code snippet which we need to paste in mentioned files.
      a. Replace code of IServiceMethods class with below. Here in this code you can change type of methods, parameters of method and output format. if there is any input parameter in you requirement, just add like query string in UriTemplate. 
using System.ServiceModel;
using System.ServiceModel.Web;

[ServiceContract]
public interface IServiceMethods
{
    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "SampleServiceCall")]
    string SampleServiceCall();
}

ex. if you have input parameters.
[ServiceContract]
public interface IServiceMethods
{
    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "SampleServiceCall?Data={data}")]
    string SampleServiceCall(string data);

}

    b. Replace code of ServiceMethods class with below
using System.ServiceModel.Activation;

namespace IExchangeSP13Service.Code
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public sealed class ServiceMethods : IServiceMethods
    {
        public string SampleServiceCall()
        {
            return "Success";
        }
    }  
}

c. Replace code of IExchangeService.svc with below. This code will help to host this web service. So we don't need to updated any web.config for hosting.
<%@ServiceHost   language= "C#"   
Factory= "Microsoft.SharePoint.Client.Services.MultipleBaseAddressDataServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, 
Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Service= "IExchangeSP13Service.Code.ServiceMethods" %> 

21. Service is ready. Now deploy the service.

Comments

Popular posts from this blog

C# Copy files from one server to another

Suppress StyleCop SA1600

Telerik Rad Grid Sorting