1 / 29

Interacting with SharePoint using the CSOM and REST API

Interacting with SharePoint using the CSOM and REST API. Presented by Eric Smith 2.1.2014. Special Thanks to our Platinum Sponsor. …and our Gold Sponsor. Who am I?. SharePoint developer at L-3 Communications 3 Years experience in SharePoint BS from U of U and current MBA Student

Download Presentation

Interacting with SharePoint using the CSOM and REST API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Interacting with SharePoint using the CSOM and REST API Presented by Eric Smith 2.1.2014

  2. Special Thanks to our Platinum Sponsor …and our Gold Sponsor

  3. Who am I? • SharePoint developer at L-3 Communications • 3 Years experience in SharePoint • BS from U of U and current MBA Student • MTA, MCSA, MOS, MCTS, MCITP, MCSD • Active in numerous mobile development projects

  4. Intro to CSOM and REST API

  5. SharePoint API

  6. CSOM • API for Remote applications • Designed to be similar to server-object model • Introduced in SharePoint 2010 and expanded in 2013 • Three implementations • .NET Managed, Silverlight, Javascript • Facades on top of /_vti_bin/Client.svc • Communication with SharePoint done in batches

  7. Three Implementations • .NET Managed • Located in <System Root>\ISAPI • Microsoft.SharePoint.Client.*.dll • Silverlight • Located in <System Root>\TEMPLATE\LAYOUTS\ClientBin • Microsoft.SharePoint.Client.*.Silverlight.dll • Microsoft.SharePoint.Client.*.Phone.dll • JavaScript • Located in <System Root >\TEMPLATE\LAYOUTS • SP.*.js

  8. CSOM Coverage 2010 • Site Collections, Webs, Features, Event Receivers • Lists, List Items, Fields, Content Types, Views, Forms • Files, Folders

  9. CSOM Coverage 2013 • Site Collections, Webs, Features, Event Receivers • Lists, List Items, Fields, Content Types, Views, Forms, IRM • Files, Folders • Users, Roles, Groups, User Profiles, Feeds • Web Parts • Search, Analytics • Taxonomy • Workflow • E-Discovery, Business Data (BCS)

  10. Communication With SharePoint • All CRUD operations are automatically batched • Requests for resources are matched using Load and LoadQuery • Batches are executed using ExecuteQuery or ExecuteQueryAsync • This triggers a POST request to Client.svc/ProcessQuery • Message body contains XML document with batched request information • Responses contains requested resources in JSON format

  11. Retrieving Resources Using Load • “Load” indicates specified object data should be included in next batch retrieval • Not all properties are retrieved • Primary exceptions are collections

  12. Retrieving Resources Using Load (cont.) Managed JavaScript varclientContext = SP.ClientContext.get_current(); this.oWebsite = clientContext.get_web(); clientContext.load(this.oWebsite); clientContext.executeQueryAsync( Function.createDelegate(this, this.onQuerySucceeded), this.onQueryFailed) ); function onQuerySucceeded(sender, args) { alert('Title: ' + this.oWebsite.get_title() + ' Description: ' + this.oWebsite.get_description()); } ClientContext context = new ClientContext("SiteUrl"); Web web = context.Web; context.Load(web); context.ExecuteQuery(); label1.Text = web.Title;

  13. Demo Using Load • DEMO TIME

  14. Retrieving Resources Using LoadQuery • Indicates result of query should be included in next batch retrieval • Query executed on server • Result returned from call • Not loaded in place as with Load

  15. Demo using LoadQuery • DEMO TIME

  16. REST API • What is REST API in SharePoint • Data-centric web services based on the Open Data Protocol (OData) • Each resources or set of resources is addressable • http://<Site URL>/_api/web • http://<Site URL>/_api/web/lists • http://<Site URL>/_api/web /lists/getByTitle(“Vendors”) • Operations on resources mapped to HTTP verbs • GET, PUT, POST, DELETE, … • Results are AtomPub (XML) or JSON

  17. Why REST when we have CSOM? • REST has significant industry momentum • Allows stacks other than the .NET • Simpler and Easier to Use Than SOAP • SOAP had major limitations in it’s non-flexibility, it was hard to create, consume, and maintain • Results can be returned in JSON and ATOM format • Each query is submitted with a unique URL • REST Results can be cached by proxy servers because you no longer expect different results & run two different queries using same URL • Note – currently no support for batching

  18. REST API History • SharePoint 2010 • Initial REST API added • /_vti_bin/ListData.svc • Exposed Crud operations on list data • SharePoint 2013 • REST API expands and evolves • ListData.svc deprecated • RESTfulopperations added to /_vti_bin/Client.svc • /_api added alias for /_vti_bin/Client.svc

  19. REST API Coverage • Sites, Webs, Features, Event Receivers, Site Collections • Lists, List Items, Fields, Content Types, Views, Forms, IRM • Files, Folders • Users, Roles, Groups, User Profiles, Feeds • Search

  20. Retrieving Data using REST API (Managed) • /_api does not expose metadata (well...) • You cannot add a service reference from visual studio • Two options • Get data in XML format and use LINQ to XML • Get data in JSON format and use built-in or third party serialization • JavascriptSerializer, JSON.NET

  21. How Authentication Works

  22. REST API Managed Demo • DEMO TIME

  23. OData URL

  24. Odata Examples • Available Collections • http://services.odata.org/Northwind/Northwind.svc/ • Metadata: • http://services.odata.org/Northwind/Northwind.svc/$metadata • Query Entity Set (Collection) • http://services.odata.org/Northwind/Northwind.svc/Customers • Customer With Single Entry • http://services.odata.org/Northwind/Northwind.svc/Customers('LETSS') • Get One Property: • http://services.odata.org/Northwind/Northwind.svc/Customers('LETSS')/Address • Value of a Property: • http://services.odata.org/Northwind/Northwind.svc/Customers('LETSS')/Address/$value • Collection of related Links without actual entries: • http://services.odata.org/Northwind/Northwind.svc/Customers('LETSS')/$links/Orders • Related Entries: • http://services.odata.org/Northwind/Northwind.svc/Customers('LETSS')/Orders(10643)/Order_Details

  25. Odata Query support • Options: $filter, $sort, $orderby, $top, $expand, $skip, $take, $metadata… • Operations: eq, ne, gt, ge, lt, le, and, or, not, mod, add, sub • Functions: startswith, substring, replace, tolower, trim, round, ceiling, day, month, year, typeof,… • Top 3 Customer from USA Order By ContactName • http://services.odata.org/Northwind/Northwind.svc/Customers?$filter=Country eq 'USA'&$orderby=ContactName&$top=3 • Return related Entries Inline • http://services.odata.org/Northwind/Northwind.svc/Customers('LETSS')/$links/Orders • Get Data in JSON Format • http://services.odata.org/Northwind/Northwind.svc/Customers('LETSS')?$expand=Orders&$format=JSON

  26. SharePoint Examples • _api => _vti_bin/client.svc • _api/web/lists • _api/web/lists/lists(guid) • _api/web/lists/getByTitle(‘Announcements’) • _api/web/lists/getbytitle(‘Announcements’)/items(2)/FieldValuesAsHtml/$select=Title,Author • _api/web/getAvailableWebTemplates(lcid=1033) • _api/web/?$select=title,id • _api/web/lists/getByTitle(‘mylist’)?$select=title,firstname • _api/web/lists/getByTitle(‘customers’)?$select=title,firstname&startswith(Title, ‘p’)

  27. Working with Documents and Libraries

  28. Great Resources / References • Side by side .NET Server, CSOM, JSOM, and REST API • http://msdn.microsoft.com/en-us/library/office/dn268594.aspx • Choose the right API set in SharePoint 2013 • How to: Complete basic operations using SharePoint 2013 client library code • How to: Complete basic operations using JavaScript library code in SharePoint 2013 • How to: Access SharePoint 2013 data from remote apps using the cross-domain library • Programming using the SharePoint 2013 REST service • How to: Complete basic operations using SharePoint 2013 REST endpoints • Host webs, app webs, and SharePoint components in SharePoint 2013 • http://www.odata.org/

  29. Wrap up • Strongly suggest Rob Windsor courses on Pluralsight • All demo files can be downloaded here: • http://sdrv.ms/LzRDZd • Contact me via Eric[At]2020BI.com • Thanks again

More Related