You are on page 1of 21

REST and CSOM in SharePoint 2013

Sahil Malik
www.winsmarts.com
Our SPJourney
SharePoint 2013 Basics SharePoint 2013 Behind the Scenes

An Overview of SharePoint Development

PowerShell WSPs Apps

Application
Modules Pages
Feature
Pages Receivers
WebParts Site Pages

PowerShell List Definitions

CAML & LINQ Content Types


Our SPJourney
SharePoint 2013 Basics SharePoint 2013 Behind the Scenes

An Overview of SharePoint Development

PowerShell WSPs Apps

Application Client side technologies


Modules Pages
Feature
Pages Receivers
WebParts Site Pages

PowerShell List Definitions

CAML & LINQ Content Types


What will I cover here?

Why Microsoft created REST and CSOM?


Why you need to learn it?
CSOM examples and basics
REST examples and basics
Client side technologies in SharePoint 2013

REST Representational State Transfer


CSOM Client side Object model
WCF Windows communication Foundation
WebApi
Why Microsoft created REST and CSOM?

SharePoint = sausage factory


Lots of moving parts inside
Lots of intricacies
Difficult to do code isolation, sandbox solutions was an attempt.
Farm solution code runs under full trust (in SharePoint 2013)
Deploying is a pain, you need to be nice to your IT Ogres
Cleaning up and Upgrades are a bigger pain
Office 365, were getting all cloudy
How do you support thousands of tenants?
How do you offer them a development story?
Third party integration story
Why Microsoft created REST and CSOM

The need for distributed computer systems.


Need for mobile devices and apps on various platforms.
Rich applications running inside the browser.
Apps (SharePoint Apps), cannot run server side code on the
SharePoint server.
So, the programming experience for Apps is REST and CSOM.
Why do you need to learn about REST and CSOM?

80% of the code you will write in SharePoint 2013 will be Apps.
100% of that app-code, will use CSOM and REST.
THAT IS WHY!
And even if you dont intend to write Apps,
REST and CSOM is the preferred way of delivering functionality.
If it works using REST and CSOM, it will work as a sandbox solution, and it
will work in Office 365*
REST and CSOM are here to stay
Introduced in SharePoint 2010
Greatly expanded API in SharePoint 2013
Also added security constructs to support Apps

* There are some minor differences in the REST/CSOM API in Office 365
Synchronous vs. Asynchronous

var data = DownloadData(...);


ProcessData(data);

DownloadDataAsync(... , data => {


ProcessData(data);
});
Asynch Code challenges Error Handling

How do you do error handling?

DownloadDataAsync(... , data => {


ProcessData(data);
});

FailHandler
DownloadDataAsync
(ex. TimeOutException)

ProcessData
Asynch Code Challenges Timing issues
Asynch Code Challenges - Concurrency
Async code challenges

Error handling
Different way of structuring your code, depending upon your platform of
choice.
Timing issues
Async/Await patterns, or Promises but something you have to consciously
architect and plan for.
Re-entrancy is another thing you have to guard against.
Concurrency
A lot of support is built into the API.
But you do have to understand how it works.
Concurrency, so how does it work?

The challenge
Multiple users updating the same data at the same time.
Who wins?
Approaches
Lock data on reads
Cant use this approach in a disconnected/distributed scenario.
Check for changes on save
The data that I am saving, does the current state of the data match what I had
queried?
Do I compare only changed columns, or all columns?
What if I do multiple queries, before one save?
Object Identity
The general theme

Query Data Work with data Save your changes

Exception
Handlers
CSOM/REST comparison

What CSOM REST


Going to the server ExecuteQuery $.get
ExecuteQueryAsync $.getJSON
$.ajax
Concurrency Checks built into framework during save operations. eTAG support
Object Identity & Object Tracking
Data Objects Objects that inherit from ClientObject JavaScript objects, or similar non-intelligent
business objects
Usual application Usually C# etc. (server side code in apps) Usually browser side code (usually JavaScript)

Security Piggy backs on transport security + OAuth Piggy backs on browser security + OAuth

Community support SharePoint specific some support. JavaScript and jQuery and REST are open
standards lots of support.
SharePoint REST and CSOM history

SharePoint 2007 SharePoint 2010 SharePoint 2013

Web Services Client Object High


Model expanded
REST API and
integrated
CSOM and
REST API
SharePoint 2010

_vti_bin/client.svc and _vti_bin/listdata.svc

Server
Execute
Client OData Query

JavaScript Silverlight .NET CLR


Library Library Library

Custom Client Code


SharePoint 2013

Support for SharePoint Security Model

_vti_bin/client.svc and _vti_bin/listdata.svc


_api

Server
Execute
Client OData Query

JavaScript Silverlight .NET CLR


Library Library Library

Custom Client Code


What can you use CSOM and REST in?
Summary and Whats next?

A basic intro to REST and CSOM


Deeper dive into CSOM
Deeper dive into REST

Next course: Apps

You might also like