hard learned csom and rest tips
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.TRANSCRIPT
![Page 1: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/1.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/2.jpg)
sponsors
![Page 3: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/3.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/4.jpg)
Hard learned SharePoint dev tips Hard learned CSOM and REST
0945AM 0345PM
![Page 5: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/5.jpg)
Client side technologies in SharePoint 2013
![Page 6: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/6.jpg)
Why Microsoft created REST and CSOM?
![Page 7: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/7.jpg)
Why Microsoft created REST and CSOM
![Page 8: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/8.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/9.jpg)
Synchronous vs. Asynchronous
var data = DownloadData(...);ProcessData(data);
DownloadDataAsync(... , data => {ProcessData(data);
});
![Page 10: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/10.jpg)
Synchronous vs. Asynchronous
var data = DownloadData(...);ProcessData(data);
DownloadDataAsync(... , data => {ProcessData(data);
});
![Page 11: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/11.jpg)
Asynch Code challenges – Error Handling
DownloadDataAsync(... , data => {ProcessData(data);
});
![Page 12: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/12.jpg)
Asynch Code Challenges – Timing issues
![Page 13: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/13.jpg)
Asynch Code Challenges - Concurrency
![Page 14: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/14.jpg)
Async code challenges
![Page 15: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/15.jpg)
Concurrency, so how does it work?
![Page 16: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/16.jpg)
The general theme
![Page 17: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/17.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/18.jpg)
CSOM
![Page 19: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/19.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/20.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/21.jpg)
ClientContext
![Page 22: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/22.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/23.jpg)
Load vs LoadQuery
![Page 24: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/24.jpg)
Update Data
![Page 25: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/25.jpg)
ExecuteQuery vs ExecuteQueryAsync
![Page 26: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/26.jpg)
Upload file using CSOM
![Page 27: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/27.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/28.jpg)
How to use CSOM? – Windows Phone
![Page 29: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/29.jpg)
How to use CSOM? – JavaScript
![Page 30: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/30.jpg)
Referencing CSOM by hand
![Page 31: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/31.jpg)
Get and Set Web properties
![Page 32: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/32.jpg)
Get List Items
![Page 33: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/33.jpg)
Method Chaining
![Page 34: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/34.jpg)
CRUD Operations
![Page 35: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/35.jpg)
REST API
![Page 36: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/36.jpg)
What is REST?
![Page 37: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/37.jpg)
REST Data Formats
![Page 38: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/38.jpg)
REST API
![Page 39: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/39.jpg)
SharePoint 2013 REST URL structure
http://server/site/_api/site
![Page 40: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/40.jpg)
SharePoint REST API Parameter structure
http://server/site/_api/web/getAvailableWebTemplates(lcid=1033, includeCrossLanguage=true)
![Page 41: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/41.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/42.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/43.jpg)
Query Operator Support
![Page 44: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/44.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/45.jpg)
Example
![Page 46: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/46.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/47.jpg)
HTTP Methods
![Page 48: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/48.jpg)
HTTP Methods
![Page 49: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/49.jpg)
Form Digest
![Page 50: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/50.jpg)
/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](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/51.jpg)
Traversing Folders
![Page 52: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/52.jpg)
CRUD Operations
![Page 53: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/53.jpg)
Create a site
![Page 54: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/54.jpg)
Create a list
![Page 55: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/55.jpg)
Create a file in a doc lib
Create a file in a folder
![Page 56: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/56.jpg)
Update – Either PUT or PATCH(MERGE)
![Page 57: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/57.jpg)
Update using PUT
![Page 58: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/58.jpg)
Update using PATCH
![Page 59: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/59.jpg)
Getting contents of a file
![Page 60: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/60.jpg)
Deletes
![Page 61: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/61.jpg)
ETags
![Page 62: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/62.jpg)
Get Changes since last refresh
![Page 63: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/63.jpg)
REST Error Diagnosis
![Page 64: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/64.jpg)
questions?
HTTP://BLAH.WINSMARTS.COM
@SAHILMALIK
![Page 65: Hard learned CSOM and REST tips](https://reader030.vdocument.in/reader030/viewer/2022013114/54662e5eaf795983338b5067/html5/thumbnails/65.jpg)
thank you.
SHAREPOINT AND PROJECT CONFERENCE ADRIATICS 2013ZAGREB, NOVEMBER 27-28 2013