+ All Categories
Home > Technology > Hard learned CSOM and REST tips

Hard learned CSOM and REST tips

Date post: 12-Nov-2014
Category:
Upload: sharepoint-project-conference-adriatics
View: 2,052 times
Download: 3 times
Share this document with a friend
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.
65
SHAREPOINT AND PROJECT CONFERENCE ADRIATICS 2013 ZAGREB, NOVEMBER 27-28 2013 Hard learned CSOM and REST SAHIL MALIK WWW.WINSMARTS.COM @SAHILMALIK
Transcript
Page 1: Hard learned CSOM and REST tips

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

Hard learned CSOM and REST

SAHIL MALIK

WWW.WINSMARTS.COM

@SAHILMALIK

Page 2: Hard learned CSOM and REST tips

sponsors

Page 3: Hard learned CSOM and REST tips

C:\>whoami

11xMVP

15xAuthor

Pluralsight Author

Microsoft Metro Trainer

Funny and Honest

HTTP://BLAH.WINSMARTS.COM@SAHILMALIK

Page 4: Hard learned CSOM and REST tips

Hard learned SharePoint dev tips Hard learned CSOM and REST

0945AM 0345PM

Page 5: Hard learned CSOM and REST tips

Client side technologies in SharePoint 2013

Page 6: Hard learned CSOM and REST tips

Why Microsoft created REST and CSOM?

Page 7: Hard learned CSOM and REST tips

Why Microsoft created REST and CSOM

Page 8: Hard learned CSOM and REST tips

Why do you need to learn about REST and CSOM?

* There are some minor differences in the REST/CSOM API in Office 365

Page 9: Hard learned CSOM and REST tips

Synchronous vs. Asynchronous

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

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

});

Page 10: Hard learned CSOM and REST tips

Synchronous vs. Asynchronous

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

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

});

Page 11: Hard learned CSOM and REST tips

Asynch Code challenges – Error Handling

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

});

Page 12: Hard learned CSOM and REST tips

Asynch Code Challenges – Timing issues

Page 13: Hard learned CSOM and REST tips

Asynch Code Challenges - Concurrency

Page 14: Hard learned CSOM and REST tips

Async code challenges

Page 15: Hard learned CSOM and REST tips

Concurrency, so how does it work?

Page 16: Hard learned CSOM and REST tips

The general theme

Page 17: Hard learned CSOM and REST tips

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.

Page 18: Hard learned CSOM and REST tips

CSOM

Page 19: Hard learned CSOM and REST tips

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

Page 20: Hard learned CSOM and REST tips

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

Page 21: Hard learned CSOM and REST tips

ClientContext

Page 22: Hard learned CSOM and REST tips

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

Page 23: Hard learned CSOM and REST tips

Load vs LoadQuery

Page 24: Hard learned CSOM and REST tips

Update Data

Page 25: Hard learned CSOM and REST tips

ExecuteQuery vs ExecuteQueryAsync

Page 26: Hard learned CSOM and REST tips

Upload file using CSOM

Page 27: Hard learned CSOM and REST tips

How to use CSOM? – Silverlight

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

Page 28: Hard learned CSOM and REST tips

How to use CSOM? – Windows Phone

Page 29: Hard learned CSOM and REST tips

How to use CSOM? – JavaScript

Page 30: Hard learned CSOM and REST tips

Referencing CSOM by hand

Page 31: Hard learned CSOM and REST tips

Get and Set Web properties

Page 32: Hard learned CSOM and REST tips

Get List Items

Page 33: Hard learned CSOM and REST tips

Method Chaining

Page 34: Hard learned CSOM and REST tips

CRUD Operations

Page 35: Hard learned CSOM and REST tips

REST API

Page 36: Hard learned CSOM and REST tips

What is REST?

Page 37: Hard learned CSOM and REST tips

REST Data Formats

Page 38: Hard learned CSOM and REST tips

REST API

Page 39: Hard learned CSOM and REST tips

SharePoint 2013 REST URL structure

http://server/site/_api/site

Page 40: Hard learned CSOM and REST tips

SharePoint REST API Parameter structure

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

Page 41: Hard learned CSOM and REST tips

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"}

Page 42: Hard learned CSOM and REST tips

REST service syntax for parameters in query string

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

Page 43: Hard learned CSOM and REST tips

Query Operator Support

Page 44: Hard learned CSOM and REST tips

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

Page 45: Hard learned CSOM and REST tips

Example

Page 46: Hard learned CSOM and REST tips

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' }

Page 47: Hard learned CSOM and REST tips

HTTP Methods

Page 48: Hard learned CSOM and REST tips

HTTP Methods

Page 49: Hard learned CSOM and REST tips

Form Digest

Page 50: Hard learned CSOM and REST tips

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

Page 51: Hard learned CSOM and REST tips

Traversing Folders

Page 52: Hard learned CSOM and REST tips

CRUD Operations

Page 53: Hard learned CSOM and REST tips

Create a site

Page 54: Hard learned CSOM and REST tips

Create a list

Page 55: Hard learned CSOM and REST tips

Create a file in a doc lib

Create a file in a folder

Page 56: Hard learned CSOM and REST tips

Update – Either PUT or PATCH(MERGE)

Page 57: Hard learned CSOM and REST tips

Update using PUT

Page 58: Hard learned CSOM and REST tips

Update using PATCH

Page 59: Hard learned CSOM and REST tips

Getting contents of a file

Page 60: Hard learned CSOM and REST tips

Deletes

Page 61: Hard learned CSOM and REST tips

ETags

Page 62: Hard learned CSOM and REST tips

Get Changes since last refresh

Page 63: Hard learned CSOM and REST tips

REST Error Diagnosis

Page 64: Hard learned CSOM and REST tips

questions?

HTTP://BLAH.WINSMARTS.COM

@SAHILMALIK

Page 65: Hard learned CSOM and REST tips

thank you.

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


Recommended