Hard learned CSOM and REST tips


Published on

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.

Published in: Technology

Hard learned CSOM and REST tips

  2. 2. sponsors
  3. 3. C:>whoami 11xMVP 15xAuthor Pluralsight Author Microsoft Metro Trainer Funny and Honest HTTP://BLAH.WINSMARTS.COM @SAHILMALIK
  4. 4. Hard learned SharePoint dev tips 0945AM Hard learned CSOM and REST 0345PM
  5. 5. Client side technologies in SharePoint 2013
  6. 6. Why Microsoft created REST and CSOM?
  7. 7. Why Microsoft created REST and CSOM
  8. 8. Why do you need to learn about REST and CSOM? * There are some minor differences in the REST/CSOM API in Office 365
  9. 9. Synchronous vs. Asynchronous var data = DownloadData(...); ProcessData(data); DownloadDataAsync(... , data => { ProcessData(data); });
  10. 10. Synchronous vs. Asynchronous var data = DownloadData(...); ProcessData(data); DownloadDataAsync(... , data => { ProcessData(data); });
  11. 11. Asynch Code challenges – Error Handling DownloadDataAsync(... , data => { ProcessData(data); });
  12. 12. Asynch Code Challenges – Timing issues
  13. 13. Asynch Code Challenges - Concurrency
  14. 14. Async code challenges
  15. 15. Concurrency, so how does it work?
  16. 16. The general theme
  17. 17. CSOM/REST comparison What 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.
  18. 18. CSOM
  19. 19. How CSOM works Create a ClientContext Use Load/LoadQuery to specify what data you wish to get Get data using ExecuteQuery/ExecuteQueryAsync Make changes to data Save data using ExecuteQuery/ExecuteQueryAsync
  20. 20. How CSOM works Client Application Server Sequence of commands: Client.svc command 1; command 2; command 3; context.ExecuteQuery(); Process results XML JSON Execute commands in the batch: command 1; command 2; command 3; Send results back
  21. 21. ClientContext
  22. 22. Client Objects Server (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
  23. 23. Load vs LoadQuery
  24. 24. Update Data
  25. 25. ExecuteQuery vs ExecuteQueryAsync
  26. 26. Upload file using CSOM
  27. 27. How to use CSOM? – Silverlight http://blah.winsmarts.com/2010-3-Integrating_Silverlight_BING_Maps_with_SharePoint_2010.aspx
  28. 28. How to use CSOM? – Windows Phone
  29. 29. How to use CSOM? – JavaScript
  30. 30. Referencing CSOM by hand
  31. 31. Get and Set Web properties
  32. 32. Get List Items
  33. 33. Method Chaining
  34. 34. CRUD Operations
  35. 35. REST API
  36. 36. What is REST?
  37. 37. REST Data Formats
  38. 38. REST API
  39. 39. SharePoint 2013 REST URL structure http://server/site/_api/site
  40. 40. SharePoint REST API Parameter structure http://server/site/_api/web/getAvailableWebTemplates(lcid=1033, includeCrossLanguage=true)
  41. 41. 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"}
  42. 42. REST service syntax for parameters in query string http://<server>/<site>/_api/web/applyWebTemplate?template=”STS#0”
  43. 43. Query Operator Support
  44. 44. REST Query Operators Supported Numeric comparisons •Lt •Le •Gt •Ge •Eq •Ne String comparisons •startsWith •substringof •Eq •Ne Date and time functions •day() •month() •year() •hour() •minute() •second() Not supported •Arithmetic operators (Add, Sub, Mul, Div, Mod) •Basic math functions (round, floor, ceiling) •endsWith •replace •substring •tolower •toupper •trim •concat •DateTimeRangesOverlap operator •Querying as to whether a date time falls inside a recurrent date time pa
  45. 45. Example
  46. 46. Example Queries Description 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=Ti tle 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' }
  47. 47. HTTP Methods
  48. 48. HTTP Methods
  49. 49. Form Digest
  50. 50. /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.
  51. 51. Traversing Folders
  52. 52. CRUD Operations
  53. 53. Create a site
  54. 54. Create a list
  55. 55. Create a file in a doc lib Create a file in a folder
  56. 56. Update – Either PUT or PATCH(MERGE)
  57. 57. Update using PUT
  58. 58. Update using PATCH
  59. 59. Getting contents of a file
  60. 60. Deletes
  61. 61. ETags
  62. 62. Get Changes since last refresh
  63. 63. REST Error Diagnosis