REST or JSOM – Scenario based Usage

In this article we can see the when to use REST and when to use JSOM depending on the scenario.

image

REST

REST is Representational State Transfer. REST is an architectural style and uses HTTP protocol. SharePoint 2013 onwards REST API is supported.

For filtering data, REST supports OData protocol.

One of the example of REST URL is given below:

http://server/_api/web/lists

You can find more REST examples from References.

JSOM

JSOM (JavaScript Object Model) is CSOM (Client Side Object Model) of interacting with SharePoint. This programming way is available from SharePoint 2010 onwards. JSOM Programming requires the following:

1. Deeper understanding of the SharePoint CSOM API

2. Reference to JSOM JavaScript files

3. CAML query used for filtering

Following is an example code for JSOM:

var listTitle = “Contacts”;

using(var context = new ClientContext(“http://url”))

{

var list = context.Web.Lists.GetByTitle(listTitle);

context.Load(list);

context.ExecuteQuery();

}

image

You can see References for source-code example.

REST Advantages

Following are the advantages of going with REST:

1. REST web requests are easier to work with that we can use Browser to test them

2. No overheads of WSDL creation Or Detailed SharePoint knowledge required

3. No overheads of tying up with Service Reference

4. Direct Calls for fetch, insert, update, delete, find operations

5. Better Interoperability supporting different clients on different platforms, programming models

In this way REST is more preferred among SharePoint Programmers.

JSOM Advantages

Following are the advantages of JSOM over REST which I would prefer to use only in particular scenarios.

1. JSOM supports Batching.

Batching is the technique of tying multiple queries and sending to server. In this way, the roundtrip can be reduced to one.

Scenario: You have a Web Page where 10 list items needs to be loaded. In the case of REST, this will require 10 roundtrips to the server. Right?

But if you are using JSOM, you can wrap all the queries into one roundtrip. Invoke the Load() method multiple times as shown below.

context.Load(list1);

context.Load(list2);

context.Load(list10);

Then we can call the ExecuteQuery() method which will actually send the query to the server. This is the advantage of Batching in JSOM.

image

image

Chattier is another term which means the Opposite of Batching. Chattier means it requires chat like request-response between client & server. So REST is chattier & JSOM not.

References

http://www.jeanpaulva.com/index.php/2015/01/10/sharepoint-rest/

http://www.c-sharpcorner.com/UploadFile/40e97e/jsom-batching-code-example/

Summary

In this article we can have explored the usage of REST vs. JSOM based on scenario.

One thought on “REST or JSOM – Scenario based Usage

  1. PK says:

    Excellent article Jean. I have always been confused about when to use JSOM and REST. REST is definitely chattier but is useful for quick lookups. JSOM, on the other hand is batchy, but is somewhat slow in response time. Has that been your observation as well?

Leave a Reply

Your email address will not be published. Required fields are marked *