Get folder and files recursively in SharePoint using REST call

There was a simple requirement I came across for which a page should list all the files and folders in a SharePoint document library.

I came up with a solution using ODATA call. Following is the REST call I made to get the list of all folder and files.

/_api/web/Lists/GetByTitle(‘Documents’)/Items?$select=FileLeafRef,FileRef&$orderby=FileRef asc
Following is the code which quickly prints them out in a table. Just add this code to a script editor web-part.
Please make sure the URL is updated based on your site URL.


WCF Service with SOAP, REST JSON & REST XML – 3 in 1 !

WCF Service with SOAP, REST JSON & REST XML – 3 in 1 !

WCF has evolved a lot in the past few years. Currently expectations from end user are that, they should be able to use applications across multiple devices, be it Windows, Mac, mobile devices or web.

Service Oriented Architecture (SOA) is the right architecture when we have to deal with such a scenario. SOA helps multiple type of applications to consume and process data without the need to implement the back-end data access or business logics. Be it a Windows/OS-X desktops application or iOS/Android/Windows mobile applications.

In the case of Windows/ASP.NET web applications, web-service can expose SOAP based service which can be consumed and implemented easily using existing .NET frameworks.

For Mac applications REST as well SOAP can be consumed. Though SOAP consumption requires non traditional methods. With development tools like Xamarin it’s even easier.

For Mobile applications be it iOS based or Android based or Windows Mobile based or even Java based, they can consume REST based service in JSON/XML format and can be implemented easily.

Finally for web based applications we have Jquery which has direct support for JSON.

This post will explain how to enable all the three SOAP, REST JSON & REST XML in WCF Service along with a sample source code.

Let’s start by creating a WCF Service Application using Visual Studio and .NET Framework 4.5. You can use even Framework 4.0 or 3.5


Let’s modify interface so that operation contracts support REST Get and POST operations

  1. Set Attributes for each OperationContract (System.ServiceModel.Web.WebInvoke or System.ServiceModel.Web.WebGet) along with URI Template

  1. For WebInvoke set Attribute with Method, UriTemplate & BodyStyle as Bare

Setting BodyStyle as Bare will allow the service to expect and respond with data type (json/xml) based on the “content-type: application/json or content-type: application/xml” which should be set as part of incoming request’s header.

Final Interface Code (Look into the highlighted lines)


Let’s modify the web.config so that it can support both SOAP as well as REST and allow the service to auto select the REST request and response formats.

For SOAP, add service behavior with name “DefaultServiceBehavior“. This should be under <system.servicemodel><behaviors><serviceBehaviors>

For REST, add endpoint behavior with name “RESTEndPointBehavior“. This should be under <system.servicemodel><behaviors><endpointBehaviors>

Make sure automaticFormatSelectionEnabled is set to true in webHttp tag. This will allow the service to expect and respond with data type (json/xml) based on the “content-type: application/json or content-type: application/xml” which should be set as part of incoming request’s header.

Finally add service under <system.servicemodel><services>

The endpoint SoapService uses basicHttpBinding where as endpoint RestService uses webHttpBinding.

The RESTEndPointBehavior behavior configuration will make sure the service uses automatic format selection.

Hence when the incoming request’s header has “content-type: application/json”, the service will expect a json based request data and respond with json data. If it has “content-type: application/xml”, the service will expect a xml based request data and respond with xml data.

Final Web.config code (Look into the highlighted lines)

Now let’s look into the part where the hosted service can be consumed and see if it works fine.


Launch the WCF Test Client using Visual Studio Command Prompt
Add the service and call each methods with appropriate inputs. You should be able to see a result similar to below.

For REST we will use Fiddler to compose and analyze the request and response.

Launch the fiddler, use the screenshots below to compose a request. Make a note that content-type header decides the message format.

Also the URL should end with “RestService” then operation

e.g http://localhost:5469/TestService.svc/RestService/GetData/?value=1





Note: In all the above scenarios, the input and output use the same data format.

WCF – The incoming message has an unexpected message format ‘Raw’

I was testing out my own WCF REST Service using Fiddler and was getting error response “The incoming message has an unexpected message format ‘Raw'”.

After spending time diagnosing the message the clue was, the WCF was getting a request with no content type.

My contract was specifically expecting the request format to be XML using the following attribute

Hence added the content type as “application/xml” and things went fine.

The following is an example of Fiddler’s request for a REST POST with composite data type

Type: POST
URL: http://localhost:1510/GalleryService.svc/RestService/GetDataUsingDataContract/
Protocol: HTTP/1.1

Request Header:

User-Agent: Fiddler
Host: localhost:1510
Content-Length: 198
Content-Type: application/xml

Request Body: