REST Or JSOM - Scenario Based Usage

REST or JSOM

REST

REST is Representational State Transfer. It 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 a CSOM (Client Side Object Model) 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.

Here's an example code for JSOM:

  1. var listTitle = "Contacts";  
  2. using(var context = new ClientContext(“http://url”))  
  3. {  
  4.    var list = context.Web.Lists.GetByTitle(listTitle);  
  5.    context.Load(list);  
  6.    context.ExecuteQuery();  
  7. }  
Note: You can see References for source-code example.

REST Advantages

The 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, and 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

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

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 need 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.

  1. context.Load(list1);  
  2. context.Load(list2);  
  3. 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.

REST vs JSOM

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

References 

Summary

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