Hard learned CSOM and REST tips

Preview:

DESCRIPTION

CSOM and REST should probably be #1 on your list of things to learn. You’ve probably seen those tutorials, copy pasted code, the basic hello world apps. Well life is a lot different when writing a relatively complex app. How do you do error handling, concurrency checks, thread synchronization, memory tracking and optimization, and browser independence while taking advantage of newer browsers. Let’s not forget the pain writing and debugging your JavaScript can be, unless of course you structure it right. But how is a developer to match all those parenthesis and semicolons, or check for variable types where your best dev. environment is F12? This session is a hands-on dive into such fun topics.

Citation preview

SHAREPOINT AND PROJECT CONFERENCE ADRIATICS 2013ZAGREB, NOVEMBER 27-28 2013

Hard learned CSOM and REST

SAHIL MALIK

WWW.WINSMARTS.COM

@SAHILMALIK

sponsors

C:\>whoami

11xMVP

15xAuthor

Pluralsight Author

Microsoft Metro Trainer

Funny and Honest

HTTP://BLAH.WINSMARTS.COM@SAHILMALIK

Hard learned SharePoint dev tips Hard learned CSOM and REST

0945AM 0345PM

Client side technologies in SharePoint 2013

Why Microsoft created REST and CSOM?

Why Microsoft created REST and CSOM

Why do you need to learn about REST and CSOM?

* 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);

});

Synchronous vs. Asynchronous

var data = DownloadData(...);ProcessData(data);

DownloadDataAsync(... , data => {ProcessData(data);

});

Asynch Code challenges – Error Handling

DownloadDataAsync(... , data => {ProcessData(data);

});

Asynch Code Challenges – Timing issues

Asynch Code Challenges - Concurrency

Async code challenges

Concurrency, so how does it work?

The general theme

CSOM/REST comparisonWhat CSOM REST

Going to the server ExecuteQuery

ExecuteQueryAsync

$.get

$.getJSON

$.ajax

Concurrency Checks built into framework during save

operations.

Object Identity & Object Tracking

eTAG support

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.

CSOM

How CSOM works

Create a ClientContext

Use Load/LoadQuery to specify what data you wish to get

Make changes to data

Get data using ExecuteQuery/ExecuteQueryAsync

Save data using ExecuteQuery/ExecuteQueryAsync

How CSOM works

XML

JSON

Execute commands

in the batch:

Client.svcSequence of

commands:

context.ExecuteQuery();

command 1;

command 2;

command 3;

Send results back

command 1;

command 2;

command 3;

Process results

ServerClient Application

ClientContext

Client ObjectsServer

(Microsoft

.SharePoint)

.NET Managed

(Microsoft.SharePoint

.Client)

Silverlight

(Microsoft.SharePoint

.Client.Silverlight)

JavaScript(SP.js)

SPContext ClientContext ClientContext ClientContext

SPSite SPSite SPSite Site

SPWeb SPWeb SPWeb Web

SPList SPList SPList List

SPListItem SPListItem SPListItem ListItem

SPField SPField SPField Field

SPWeb.Title SPWeb.Title SPWeb.Title web.get_Title();

web.set_Title();

Member names mostly the same from server to client

(e. g., SPWeb.QuickLaunchEnabled = SPWeb.QuickLaunchEnabled)

JavaScript frequently has get_ set_ methods instead of properties

Load vs LoadQuery

Update Data

ExecuteQuery vs ExecuteQueryAsync

Upload file using CSOM

How to use CSOM? – Silverlight

http://blah.winsmarts.com/2010-3-Integrating_Silverlight_BING_Maps_with_SharePoint_2010.aspx

How to use CSOM? – Windows Phone

How to use CSOM? – JavaScript

Referencing CSOM by hand

Get and Set Web properties

Get List Items

Method Chaining

CRUD Operations

REST API

What is REST?

REST Data Formats

REST API

SharePoint 2013 REST URL structure

http://server/site/_api/site

SharePoint REST API Parameter structure

http://server/site/_api/web/getAvailableWebTemplates(lcid=1033, includeCrossLanguage=true)

SharePoint REST service parameter aliasing syntax

http://server/site/_api/userProfiles/People(7)/GetWorkplace(@address)?@address={"__metadata":{"type:

"ODataDemo.Address"},"Street":"NE 228th", "City":"Sammamish","State":"WA","ZipCode":"98074","Country":

"USA"}

REST service syntax for parameters in query string

http://<server>/<site>/_api/web/applyWebTemplate?template=”STS#0”

Query Operator Support

REST Query OperatorsSupported Not supported

Numeric comparisons•Lt•Le•Gt•Ge•Eq•Ne

•Arithmetic operators

(Add, Sub, Mul, Div, Mod)•Basic math functions

(round, floor, ceiling)

String comparisons•startsWith•substringof•Eq•Ne

•endsWith•replace•substring•tolower•toupper•trim•concat

Date and time functions•day()•month()•year()•hour()•minute()•second()

•DateTimeRangesOverlap operator•Querying as to whether a date time falls inside a recurrent date time pa

Example

Example QueriesDescription URL endpoint HTTP method Body content

Retrieves the title of a list web/title GET Not applicable

Retrieves all lists on a site lists GET Not applicable

Retrieves a single 'list's metadata lists/getbytitle('listname') GET Not applicable

Retrieves items within a list lists/getbytitle('listname')/items GET Not applicable

Retrieves a specific property of a document. (In

this case, the document title.)

lists/getbytitle('listname')?select=Title

GET Not applicable

Creates a list lists POST { '_metadata':{'type':SP.List}, 'AllowContentTypes': true, 'BaseTemplate': 104,

'ContentTypesEnabled': true, 'Description': 'My list description', 'Title': 'RestTest' }

Adds an item to a list lists/getbytitle('listname')/items POST

{ '_metadata':{'type':SP. listnameListItem}, 'Title': 'MyItem' }

HTTP Methods

HTTP Methods

Form Digest

/contextinfo

Property Description

webFullUrl Gets the server-relative URL of the nearest site.

siteFullUrl Gets the server-relative URL of the root of the site

collection that the site is contained within.

If the nearest web is the root of a site collection, then

the value of the webFullUrl property is equal to

the siteFullUrl property.

formDigestValue Gets the server's request form digest.

LibraryVersion Gets the current version of the REST library.

SupportedSchemaVersions Gets the versions of the schema of the REST/CSOM

library that are supported.

Traversing Folders

CRUD Operations

Create a site

Create a list

Create a file in a doc lib

Create a file in a folder

Update – Either PUT or PATCH(MERGE)

Update using PUT

Update using PATCH

Getting contents of a file

Deletes

ETags

Get Changes since last refresh

REST Error Diagnosis

questions?

HTTP://BLAH.WINSMARTS.COM

@SAHILMALIK

thank you.

SHAREPOINT AND PROJECT CONFERENCE ADRIATICS 2013ZAGREB, NOVEMBER 27-28 2013