300 likes | 474 Views
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
E N D
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 • MTA, MCSA, MOS, MCTS, MCITP, MCSD • Active in numerous mobile development projects
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
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
CSOM Coverage 2010 • Site Collections, Webs, Features, Event Receivers • Lists, List Items, Fields, Content Types, Views, Forms • Files, Folders
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)
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
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
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;
Demo Using Load • DEMO TIME
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
Demo using LoadQuery • DEMO TIME
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
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
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
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
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
REST API Managed Demo • DEMO TIME
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
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
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’)
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/
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