Interacting with SharePoint using the CSOM and REST API
Presented by Eric Smith2.1.2014
Special Thanks to our Platinum Sponsor
…and our Gold Sponsor
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
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
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
Intro to CSOM and REST API
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
SharePoint API
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
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
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
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
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
CSOM Coverage 2010
Site Collections, Webs, Features, Event Receivers Lists, List Items, Fields, Content Types, Views, Forms Files, Folders
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
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)
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
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
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
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
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
Retrieving Resources Using Load (cont.)
ManagedClientContext context = new ClientContext("SiteUrl");
Web web = context.Web;
context.Load(web);
context.ExecuteQuery();
label1.Text = web.Title;
JavaScriptvar clientContext = 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()); }
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
Demo Using Load
DEMO TIME
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
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
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
Demo using LoadQuery
DEMO TIME
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
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
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
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
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
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 RESTful opperations added to /_vti_bin/Client.svc /_api added alias for /_vti_bin/Client.svc
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
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
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
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 Javascript Serializer, JSON.NET
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
How Authentication Works
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
REST API Managed Demo
DEMO TIME
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
OData URL
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
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
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
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('L
ETSS')/$links/Orders Get Data in JSON Format
http://services.odata.org/Northwind/Northwind.svc/Customers('LETSS')?$expand=Orders&$format=JSON
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
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’)
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
Working with Documents and Libraries
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
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 lib
rary code How to: Complete basic operations using JavaScript library code in
SharePoint 2013 How to: Access SharePoint 2013 data from remote apps using the c
ross-domain library Programming using the SharePoint 2013 REST service How to: Complete basic operations using SharePoint 2013 REST en
dpoints Host webs, app webs, and SharePoint components in SharePoint
2013 http://www.odata.org/
©2012 Microsoft Corporation. All rights reserved. Content based on SharePoint 2013 Technical Preview and published July 2012.
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