You are on page 1of 12

Integration of HTTP/ HTTPs WCF Services (REST

& SOAP) in SharePoint 2013

Amin, Adnan
http://www.MSTechTalk.com
adnan.amin@hotmail.com
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
Scenario: Develop a custom web part for SharePoint 2013 which include few input fields and a file
attachment 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 in
below 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 and
CRUD 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.
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 FileTranserService
which 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();
sourceStream.Close();
}

Consuming WCF Service (SOAP) in SharePoint custom web part


I have created a visual web part in visual studio. Then I have create a simple input form which contains
few input controls including a file control.

Now add the service references to web part solution and select services.
Click on Discover button, it will show current running web services, services can also be selected by
giving full URL. Now select the service and give it a reference name like DNBServiceRefrence and press
OK. It will create a service reference for DNB Service. Similarly, create service reference for
FileTransferService.

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

The Binding details for both services will be:


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

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

<httpRuntime maxRequestLength="2097152"

The above binding details shows the binding for upload of max file size.

After doing all these configurations, you can now integrate service in custom web part to submit data to
database.

Important: The binding details defined above can be dynamically added in SharePoint sites
web.config using SPWebConfigModification class at the time of feature activation.
Consuming WCF HTTP & HTTPS REST based Service in SharePoint web part

Develop HTTP & HTTPS REST Service


REST is nothing but using the current features of the Web in a simple and effective way. In REST, we
add 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:
Important here, the endpoint binding details for REST service is defined in the Web.Config of WCF
application.

HTTP REST Service Binding

For Max file size, also define maxResuestLenght:

<httpRuntime executionTimeout="3600" maxRequestLength="2147483647"/>


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.


Consuming REST Services in SharePoint
REST service is quite simple to be integrated in SharePoint as compare to SOAP based services. They do
not require any binding details in SharePoint sites web.config. REST services are called using URL of the
methods.

Consuming HTTP REST Service


HTTP REST service can easily called in SharePoint custom web part. I have called the below code which
submitting the form data.

Above code first post file on the server, then submits data on the servicer which then saved in database
by 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 to
validate the certificate.
I have called OverrideCertificateValidation() method before service code execution. The Rest block of
code is same like HTTP REST service call.

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

<httpRuntime maxRequestLength="2097152" />

You might also like