_api: the evolution of the client object model and rest api for sharepoint 2013
DESCRIPTION
When building SharePoint 2013 Apps, developers are restricted from using the SharePoint Server Object Model. To enable you to build fully-featured solutions using the App model, Microsoft has greatly enhanced and extended the Client Object Model and REST API. This session begins with a brief introduction to the 2010 versions of these APIs and then explores how they have evolved and how to use them effectively when building SharePoint 2013 Apps or Solutions.TRANSCRIPT
_api: : The evolution of
the Client Object Model
and REST API for
SharePoint 2013
Rob Windsor
@robwindsor
About Me
• Senior SharePoint Architect with Portal Solutions
• Technical Contributor to the Pluralsight On-Demand Library
• Microsoft MVP, MCPD, MCT
• Founder and Past-President of the North Toronto .NET UG
• Co-author of Prof. Visual Basic 2012 and .NET 4.5 (Wrox)
Client(-Side) Object Model (CSOM)
• API used when building remote applications
Introduced in SharePoint 2010
Designed to be similar to the Server Object Model
• Three implementations
.NET Managed, Silverlight, JavaScript
Façades on top of /_vti_bin/Client.svc
• Communication with SharePoint done in batches
What’s New in SharePoint 2013
• Core API hasn’t changed much since 2010
• Greatly expanded coverage area
Sites, Webs, Features, Event Receivers, Site Collections
Lists, List Items, Fields, Content Types, Views, Forms
Files, Folders
Users, Roles, Groups, User Profiles, Feeds
Web Parts
Search
Taxonomy
Workflow
IRM
E-Discovery
Analytics
Business Data
REST API
• Another API used when building remote applications
• What is the REST API in SharePoint
Data-centric web services based on the Open Data Protocol (OData) Does not completely conform to OData spec
Lack of metadata is one glaring example
Each resource or set of resources is addressable
http://<site url>/_api/web
http://<site url>/_api/web/lists
http://<site url>/_api/web/lists/getByTitle(‘Customers’)
Operations on resources map to HTTP Verbs
GET, PUT, POST, DELETE, …
Results from service returned in AtomPub (XML) or JavaScript Object
Notation (JSON) format
No batching of requests
What’s New in SharePoint 2013
• ListData.svc deprecated
Still available for backwards compatibility
• RESTful operations added to /_vti_bin/Client.svc
/_api added as an alias for /_vti_bin/Client.svc
• Coverage area
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
Reading Data
• Client Object Model
Load and LoadQuery allow you to specify desired
fields/properties
Can nest includes to eager load child collections
• REST API
Use OData query syntax
Check for continuations
Creating Lists
• Client Object Model
ListCreationInformation
• REST API
POST
Body includes SP.List
Header includes Form Digest (X-RequestDigest)
SharePoint pages include Form Digest in hidden field
Creating List Items
• Client Object Model
ListItemCreationInformation
• REST API
POST
Body includes SP.Data.<List Name>ListItem
Header includes Form Digest (X-RequestDigest)
SharePoint pages include Form Digest in hidden field
Updating List Items
• Client Object Model
Same as server object model
• REST API
POST
Body includes SP.Data.<List Name>ListItem
Header includes Form Digest (X-RequestDigest)
SharePoint pages include Form Digest in hidden field
Header includes Request Method (X-Http-Method)
Set to PATCH or MERGE
Header includes concurrency option (IF-MATCH)
Set to item etag or *
Creating Custom Fields
• Client Object Model
Fields.AddFieldAsXml
Prototype field first to have SharePoint generate schema
XML for you
• REST API
POST
Body includes SP.Field<Field Type>
Header includes Form Digest (X-RequestDigest)
SharePoint pages include Form Digest in hidden field
Uploading Documents
• Client Object Model
FileCreationInformation
JavaScript
Use HTML5 FileReader to read
Use Uint8Array and SP.Base64EncodedByteArray to encode
• REST API
POST
Body contains document contents
Header includes Form Digest (X-RequestDigest)
SharePoint pages include Form Digest in hidden field
Header contains content length (Content-Length)
User Profiles
• Client Object Model
PeopleManager
Microsoft.SharePoint.Client.UserProfiles
SP.UserProfiles.js
• REST API
GET
_api/SP.UserProfiles.PeopleManager
• Apps
Requires Read permission on User Profiles
Search
• Client Object Model
KeywordQuery and SearchExecutor
Microsoft.SharePoint.Client.Search
SP.Search.js
• REST API
GET
_api/search
• Apps QueryAsUserIgnoreAppPrincipal
Calling to Host Web
• Client Object Model
Use SP.AppContextSite to get host web context
Get SPHostUrl parameter from query string
• REST API
Use _api/SP.AppContextSite(@target) to get host web
context
Get SPHostUrl parameter from query string
Use SP.RequestExecutor to make call
Web Proxy
• Client Object Model
SP.WebRequestInfo
SP.WebProxy.invoke
• REST API
POST
_api/SP.WebProxy.invoke
Body contains SP.WebRequestInfo
Thank You
• Big thanks to the organizers, sponsors and you for making
this event possible
• Please fill out your evaluation
• Please keep in touch
@robwindsor
msmvps.com/blogs/windsor