1. REST API and
Client Object
Model for
SharePoint
Developer
Ram Yadav
Sr Consultant
Microsoft Corp.
MCM : SharePoint 2007/2010
2. Agenda
Overview of accessing Data in SharePoint
Client Object Model
.NET Client Object Model
JavaScript and Client Object Model
REST & WCF Data Services
3. First , How it was 2007 and what
we have now in 2010 for
Accessing Data in SharePoint
5. Web Services
Web services are available
for use
_vit_bin/Lists.asmx
_vit_bin/Sites.asmx
_vit_bin/Webs.asmx
….
…..
6. Why Client OM
Disadvantages of Web Services
Web Services can be complicated
Difficult to call from JavaScript
Often custom wrapper services are created
Customers solve the same problem over and over
again
10. Integrating with SharePoint
Web Services
More coverage Web Services
Advanced Operations
SharePoint Server
Client Object Model Operations
Site, navigation
security services Client OM
Advanced List
Very flexible and straight forward Operations
Site Operations
REST Security
Easiest to use REST
For fixed list schema Working with list
data,
fixed schema
RPC
http://msdn.microsoft.com/en-us/library/ms478653.aspx
Server Side code , List View , Data View
11. How do I utilize client object model in my windows apps?
.NET Client object model
12. Getting Started: 3 things to know
1. ClientContext is the central object
clientContext =
new ClientContext(“http://mysite”);
2. Before you read a property, you have to
ask for it
clientContext.Load(list);
3. All requests must be committed in a batch
clientContext.ExecuteQuery();
13. Client Application Server
Sequence of Client.svc
commands:
command 1;
command 2; Execute commands
command 3; in the batch:
context.ExecuteQuery(); command 1;
command 2;
command 3;
Process results Send results back
14. Equivalent Objects
Server .NET Managed Silverlight JavaScript
(Microsoft (Microsoft.SharePoint (Microsoft.SharePoint (SP.js)
.SharePoint) .Client) .Client.Silverlight)
SPContext ClientContext ClientContext ClientContext
SPSite Site Site Site
SPWeb Web Web Web
SPList List List List
SPListItem ListItem ListItem ListItem
SPField Field Field Field
Member names mostly the same from server to client
(e. g., SPWeb.QuickLaunchEnabled = Web.QuickLaunchEnabled)
15. .Net CLR Client OM
Provides
easy access from remote .NET clients to
manipulate SharePoint data
Can be utilized from managed code – also from
office clients etc.
Assemblies
Microsoft.SharePoint.Client.dll (281kb)
Microsoft.SharePoint.Client.Runtime.dll (145kb)
To Compare: Microsoft.SharePoint.dll – 15.3MB
16. .NET Client OM Code Example 1
ClientContext clictx = new ClientContext("http://intranet.contoso.com");
Site s1 = clictx.Site;
Web w1 = clictx.Site.RootWeb;
clictx.Load(s1);
clictx.Load(w1);
clictx.ExecuteQuery();
var lists1 = clictx.LoadQuery(w1.Lists);
clictx.ExecuteQuery();
var lists2 = clictx.LoadQuery(w1.Lists.Include(l => l.Title));
clictx.ExecuteQuery();
var query1 = from l1 in w1.Lists where l1.Title != null select l1;
clictx.ExecuteQuery();
17. Client Object Model
ClientContext ctx = new ClientContext(“http://mysite”);
Web web = ctx.Site.RootWeb;
ctx.Load(web);
ctx.Load(web, w => w.Title, w => w.Description);
ctx.Load(web, w => w.Title,
w => w.Lists .Include(l => l.Fields
.Include(f => f.InternalName,
f => f.Group)));
19. Client Object Model
Authentication
Uses Windows Authentication by default
Can be configured for Claims Based Authentication
ctx.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
ctx.FormsAuthenticationLoginInfo = new
FormsAuthenticationLoginInfo("loginName", "password");
20. Client Object Model Limitations
You still need to handle synch/update semantics
(change log could help)
No elevation of privilege capabilities
Requests are throttled
.net CLR has sync method;
Silverlight CLR and Jscript are async
23. Equivalent Objects
Server .NET Managed Silverlight JavaScript
(Microsoft (Microsoft.SharePoint (Microsoft.SharePoint (SP.js)
.SharePoint) .Client) .Client.Silverlight)
SPContext ClientContext ClientContext ClientContext
SPSite Site Site Site
SPWeb Web Web Web
SPList List List List
SPListItem ListItem ListItem ListItem
SPField Field Field Field
Member names mostly the same from server to client
(e. g., SPWeb.QuickLaunchEnabled = Web.QuickLaunchEnabled)
24. JavaScript Client OM
JavaScript
Client OM is easily added to a SharePoint
ASPX page - reference:
_layouts/sp.js
Add this using <SharePoint:ScriptLink>
All libraries crunched for performance
Use un-crunched *.debug.js files with debug mode
Method signatures can be different compared to
.NET and Silverlight
Different data value types
29. Why a RESTful Interface for SP?
Why REST
It is a natural fit for SharePoint data
Item == resource
Uniform interface and addressing scheme
Low barrier of entry, interoperability
Why WCF Data Services
RESTful conventions & frameworks for data
Builds on top of standard AtomPub
Exactly the sort of convention we needed
Consistent tools and client libraries
30. A RESTful Interface for Data
Just HTTP
Items as resources, HTTP methods (GET, PUT, …) to act
Leverage proxies, authentication, ETags, …
Uniform URL convention
Every piece of information is addressable
Predictable and flexible URL syntax
Multiple representations
Use regular HTTP content-type negotiation
JSON and Atom (full AtomPub support)
32. Operations
Operations map to HTTP verbs
Retrieve items/lists GET
Create new item POST
Update an item PUT or MERGE
Delete an item DELETE
These apply to links (lookups) as well
SharePoint rules apply during updates
Validation, access control, etc.
33. URL Conventions
List of lists …/_vti_bin/listdata.svc/
List listdata.svc/Employees
Item listdata.svc/Employees(123)
Addressing lists and items
Single column listdata.svc/Employees(123)/Fullname
Lookup listdata.svc/Employees(123)/Project
traversal
Raw value listdata.svc/Employees(123)/Project/Title/$value
access
Sorting listdata.svc/Employees?$orderby=Fullname
Filtering listdata.svc/Employees?$filter=JobTitle eq 'SDE'
Projection listdata.svc/Employees?$select=Fullname,JobTitle
Paging listdata.svc/Employees?$top=10&$skip=30
Inline expansion listdata.svc/Employees?$expand=Project
35. Summary
Overview of accessing Data in SharePoint
Client Object Model
.NET Client Object Model
JavaScript and Client Object Model
REST & WCF Data Services
36. Integrating with SharePoint
Web Services
More coverage Web Services
Advanced Operations
SharePoint Server
Client Object Model Operations
Site, navigation
security services Client OM
Advanced List
Very flexible and straight forward Operations
Site Operations
Security
REST
Easiest to use REST
Working with list
For fixed list schema
data,
fixed schema
Server Side OM