WCF Service Integration With SharePoint

download WCF Service Integration With SharePoint

of 12

Transcript of WCF Service Integration With SharePoint

  • 8/11/2019 WCF Service Integration With SharePoint

    1/12

    Integration of HTTP/ HTTPs WCF Services (REST

    & SOAP) in SharePoint 2013

    Amin, Adnanhttp://www.MSTechTalk.com

    [email protected]

    http://www.mstechtalk.com/http://www.mstechtalk.com/http://www.mstechtalk.com/
  • 8/11/2019 WCF Service Integration With SharePoint

    2/12

    Contents

    WCF Service integration with SharePoint (SOAP) ......................................................................................... 2

    WCF Service Development (SOAP) ............................................................................................................ 2

    Consuming WCF Service (SOAP) in SharePoint custom web part ............................................................. 4

    Consuming WCF HTTP & HTTPS REST based Service in SharePoint web part .............................................. 7

    Develop HTTP & HTTPS REST Service ........................................................................................................ 7

    Consuming REST Services in SharePoint ................................................................................................. 10

    Consuming HTTP REST Service ............................................................................................................ 10

    Consuming HTTPS REST Service .......................................................................................................... 10

  • 8/11/2019 WCF Service Integration With SharePoint

    3/12

    Scenario: Develop a custom web part for SharePoint 2013 which include few input fields and a fileattachment control. On submitting form, data should be saved in SQL Server table using WCF service.

    WCF Service integration with SharePoint (SOAP)

    WCF Service Development (SOAP)Open visual studio 2012/2013, and create a new project, Select WCF Service Application as shown inbelow image.

    Delete the default file, and add create a new WCF Service.

    I have created a separate layer for Business Logic (class library project) for database related queries,where I have created an entity framework file and added database table. It will create entity class andCRUD operations for my entity SubDivision.

    I have added the reference of Business Layer in my service application.

    Now create a new service application, and call the AddSubdivision method to insert record in database.

  • 8/11/2019 WCF Service Integration With SharePoint

    4/12

    public void AddSubDivision(BusinessLogic. Subdivision item){

    SubdivisionManager _manager = new SubdivisionManager ();_manager.AddSubDivision(item);

    }

    In attached code solution, you can also find a service file named FileTranserServicewhich include the script to upload a file on the server.

    Now add the file upload code in Upload method:

    public void UploadFile( RemoteFileInfo request){

    FileStream targetStream = null ;Stream sourceStream = request.FileByteStream;

    string uploadFolder = @"C:\UploadFiles\" ;string filePath = Path .Combine(uploadFolder, request.FileName);

    using (targetStream = new FileStream (filePath, FileMode .Create,FileAccess .Write, FileShare .None))

    {

    //read from the input stream in 6K chunks //and save to output stream const int bufferLen = 65000;byte [] buffer = new byte [bufferLen];int count = 0;while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0){

    targetStream.Write(buffer, 0, count);}targetStream.Close();

  • 8/11/2019 WCF Service Integration With SharePoint

    5/12

    sourceStream.Close();}

    }

    Consuming WCF Service (SOAP) in SharePoint custom web partI have created a visual web part in visual studio. Then I have create a simple input form which containsfew input controls including a file control.

    Now add the service references to web part solution and select services.

  • 8/11/2019 WCF Service Integration With SharePoint

    6/12

    Click on Discover button, it will show current running web services, services can also be selected bygiving full URL. Now select the service and give it a reference name like DNBServiceRefrence and pressOK. It will create a service reference for DNB Service. Similarly, create service reference forFileTransferService .

    After adding service references, you are able to call them . But before calling them, you need to definceservice bindings and Endpoint details in SharePoint sites web.config.

    The Binding details for both services will be:

  • 8/11/2019 WCF Service Integration With SharePoint

    7/12

    You can also check the difference in binding of both services, Bind for FileTransportService include extraparameters to upload files.

    Also set the httpRuntime in web.config to max upload size:

  • 8/11/2019 WCF Service Integration With SharePoint

    8/12

    Consuming WCF HTTP HTTPS REST based Service in SharePoint web part

    Develop HTTP HTTPS REST ServiceREST is nothing but using the current features of the Web in a simple and effective way. In REST, weadd one more constraint to the current URI, every URI should uniquely represent every RESOURCE data.Below is the list of HTTP methods for getting, creating, updating and deleting a resource on the web.

    GET Get a resource

    PUT Create and update a resource

    DELETE Deletes a resource

    POST Submits data to the source

    In WCF REST service there are two type of method headers WebGet & WebInvoke, I have developed

    REST service using WebInvoke headers.Make sure to add below two namespaces in service contract file

    using System.ServiceModel;using System.ServiceModel.Web;

    Now modify service contract file as below:

    Similarly, define the file upload method

    Above scripts show the defining of service contract for REST based WCF service.

    Now, we have to define the methods in code to update the data. The methods definition is quite simple:

  • 8/11/2019 WCF Service Integration With SharePoint

    9/12

    Important here, the endpoint binding details for REST service is defined in the Web.Config of WCFapplication.

    HTTP REST Service Binding

    For Max file size, also define maxResuestLenght:

  • 8/11/2019 WCF Service Integration With SharePoint

    10/12

    HTTPS REST Service Binding

    I have used below bindings for HTTPS based service, and it worked for me.

    I also have defined maxRequestLength for maximum file upload size.

  • 8/11/2019 WCF Service Integration With SharePoint

    11/12

    Consuming REST Services in SharePointREST service is quite simple to be integrated in SharePoint as compare to SOAP based services. They donot require any binding details in SharePoint s ites web.config. REST services are called using URL of the

    methods.

    Consuming HTTP REST ServiceHTTP REST service can easily called in SharePoint custom web part. I have called the below code whichsubmitting the form data.

    Above code first post file on the server, then submits data on the servicer which then saved in databaseby WCF service.

    The service URLs can be set dynamically by multiple ways (through web part properties or web.config).

    Consuming HTTPS REST Service

    Calling of HTTPS REST service is quite similar, but it requires certificate validation, I used below code tovalidate the certificate.

  • 8/11/2019 WCF Service Integration With SharePoint

    12/12

    I have called OverrideCertificateValidation() method before service code execution. The Rest block ofcode is same like HTTP REST service call.

    Just a small modification required in SharePoint sites web.config, set the maxRequestLenth to allow themaximum upload lenght.