SharePoint REST vs CSOM
Click here to load reader
-
Upload
mark-rackley -
Category
Technology
-
view
4.047 -
download
2
description
Transcript of SharePoint REST vs CSOM
![Page 1: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/1.jpg)
Client Side Development – REST or CSOM
Mark RackleySolutions Architect
Level: Intermediate
![Page 2: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/2.jpg)
• 18+ years software architecture and development experience
• SharePoint Junkie since 2007
• Event Organizer• Blogger, Writer, Speaker• Bacon aficionado
• @mrackley• http://www.sharepointhillbilly.com
About Mark Rackley
![Page 3: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/3.jpg)
Agenda
• Pre-Game Highlights– What is CSOM and REST?
• First Quarter– API Coverage
• Second Quarter– Platforms and Standards
• Third Quarter– Ease of Use / Flexibility
• Fourth Quarter– Batch Processing / Performance
![Page 4: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/4.jpg)
ROB WINDSOR - SHAREPOINT 2013 DEVELOPMENT: CLIENT OBJECT MODEL AND REST API.
http://www.pluralsight.com/training/Courses/TableOfContents/sharepoint-2013-client-object-model-rest
![Page 5: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/5.jpg)
CLIENT SIDE OBJECT MODEL (CSOM)
![Page 6: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/6.jpg)
Client Side Object Model
Rookie Year: 2010
API used when building remote applications
• Introduced in SharePoint 2010• Designed to be similar to Server
Object Model• Three implementations
• .NET managed, Silverlight, JavaScript
• Communication with SharePoint done in batches
![Page 7: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/7.jpg)
JavaScript Client Object Model (JSOM)
context = SP.ClientContext.get_current();
var speakerList = context.get_web().get_lists().getByTitle("Vendors");
var camlQuery = SP.CamlQuery.createAllItemsQuery();
this.listItems = speakerList.getItems(camlQuery);
context.load(listItems);
context.executeQueryAsync(ReadListItemSucceeded, ReadListItemFailed);
function ReadListItemSucceeded(sender, args) {
var enumerator = listItems.getEnumerator();
var options = "<option value='0'>(None)</option>";
while (enumerator.moveNext()) {
var listItem = enumerator.get_current();
var Vendor = listItem.get_item('Vendor');
var ID = listItem.get_id();
options += "<option value='"+ ID +"'>"+Vendor+"</option>";
}
$("select[title='<Field Display Name>']").append(options);
}
function ReadListItemFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
![Page 8: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/8.jpg)
SHAREPOINT REST
![Page 9: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/9.jpg)
REST
Rookie Year: 2010
Data-centric web services based on the Open Data Protocol (OData)
• 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
![Page 10: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/10.jpg)
REST Service Access Points
• Site – http://server/site/_api/site
• Web – http://server/site/_api/web
• User Profile – http://
server/site/_api/SP.UserProfiles.PeopleManager
• Search – http:// server/site/_api/search
• Publishing – http:// server/site/_api/publishing
![Page 11: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/11.jpg)
SHAREPOINT 2010 REST
var call = $.ajax({
url: "http://<Url To Site>/_vti_bin/listdata.svc/Vendors?$select=Vendor,Id&$top=1000",
type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
}
});
call.done(function (data,textStatus, jqXHR){
var options = "<option value='0'>(None)</option>";
for (index in data.d.results) //results may exist in data.d
{
options += "<option value='"+ data.d.results[index].Id +"'>"+data.d.results[index].Title+"</option>";
}
$("select[title='<Field Display Name>']").append(options);
});
call.fail(function (jqXHR,textStatus,errorThrown){
alert("Error retrieving Tasks: " + jqXHR.responseText);
});
![Page 12: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/12.jpg)
SHAREPOINT 2013 REST
var call = $.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('ListName')/items",
type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
}
});
call.done(function (data,textStatus, jqXHR){
var options = "<option value='0'>(None)</option>";
for (index in data.d.results)
{
options += "<option value='"+ data.d.results[index].Id +"'>"+data.d.results[index].Title+"</option>";
}
$("select[title='<Field Display Name>']").append(options);
});
call.fail(function (jqXHR,textStatus,errorThrown){
alert("Error retrieving Tasks: " + jqXHR.responseText);
});
![Page 13: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/13.jpg)
REST and CSOM Behind the Scenes
![Page 14: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/14.jpg)
FIRST QUARTERAPI Coverage
![Page 15: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/15.jpg)
• 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
REST’s Roster
![Page 16: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/16.jpg)
FIELD GOAL FOR REST!It’s good!
![Page 17: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/17.jpg)
CSOM’s Roster• Sites, Webs, Features, Event Receivers, Site
Collections• Lists, List Item s, Fields, Content Types, Views, Forms,
IRM• Files, Folders• Users, Roles, Groups, User Profiles, Feeds• Web Parts• Search• Taxonomy• Workflow• E-Discovery• Analytics• Business Data
![Page 18: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/18.jpg)
TOUCHDOWN CSOM!And the extra point…. Is good!
![Page 19: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/19.jpg)
CSOM REST
7 3
0QTR
1
0 0 0:
![Page 20: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/20.jpg)
SECOND QUARTERPlatforms and Standards
![Page 21: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/21.jpg)
“REST is something Roy Fielding wrote back in 2000 that described a way to share data over HTTP. Unfortunately companies created very different implementation of RESTful services so a bunch got together to define an agreed upon protocol called the Open Data Protocol, also known as OData). The OData spec defines the data formats returned as well as the specific vocabularies used to interact with OData services. Each vendor then implemented it on their own technology stack. Microsoft did this and called their OData product WCF Data Services (notice the URL is actually odata.aspx on MSDN). SharePoint 2013's REST interface is built using WCF Data Services 5.0 which implements the OData v3.0 specification. Unfortunately the SharePoint 2013 implementation does not include everything the spec states, but it's pretty close.”
Read more at http://www.andrewconnell.com/blog/sharepoint-2013-csom-vs.-rest-...-my-preference-and-why
REST’s Playbook
![Page 22: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/22.jpg)
MSDN Magazine - Understanding and Using the SharePoint 2013 REST Interfacehttp://msdn.microsoft.com/en-us/magazine/dn198245.aspx
Use OData query operations in SharePoint REST requestshttp://msdn.microsoft.com/en-us/library/office/fp142385.aspx
Another field goal for REST!
REST’s Playbook
![Page 23: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/23.jpg)
CSOM’s Playbook
.NET, Silverlight, JavaScript
CSOM responds with a field goal!
![Page 24: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/24.jpg)
• Platform independent– PHP, Java, JavaScript, .NET, etc.. Etc
• Many Libraries to choose from that support oData
• Test queries in browser– Chrome app PostMan
REST ends the quarter strong
with Another field goal!
REST’s Playbook
![Page 25: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/25.jpg)
CSOM REST
10 9
0
QTR2
0 0 0:
![Page 26: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/26.jpg)
HALF-TIME SHOW!SPServices
![Page 27: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/27.jpg)
SPServices
jQuery library that wraps SharePoint’s .asmx Web Services in easy to call methods• Pros
– Shorter learning curve for those already comfortable with jQuery
– Cross site access– More universal Anonymous Access– Works in SharePoint 2007
• Cons– .asmx web services have been deprecated– Results returned as XML that must be manually parsed
http://spservices.codeplex.com
![Page 28: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/28.jpg)
SPServices
$().SPServices({
operation: "GetListItems",
async: true,
listName: "Vendors",
CAMLViewFields: "<ViewFields><FieldRef Name='Vendor' /></ViewFields>",
CAMLQuery: "<Query><Where><Neq><FieldRef Name='ID' /><Value
Type='Number'>0</Value></Neq></Where></Query>";,
completefunc: function(xData, Status) {
var options = "<option value='0'>(None)</option>“
$(xData.responseXML).SPFilterNode("z:row").each(function() { var Vendor = ($(this).attr("ows_Vendor"));
var ID = $(this).attr("ows_ID");
options += "<option value='"+ ID +"'>"+Vendor+"</option>";
});
$("select[title='<Field Display Name>']").append(options);
}});
![Page 29: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/29.jpg)
THIRD QUARTEREase of Use / Flexibility
![Page 30: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/30.jpg)
Working with SCOM
CAML
'<Query><Where>' +
'<Or><Or><And><Geq><FieldRef Name="StartDate" /><Value IncludeTimeValue="TRUE" Type="DateTime"> 2013-11-01 ‘+
‘</Value></Geq><Leq><FieldRef Name="StartDate" /><Value IncludeTimeValue="TRUE" Type="DateTime"> 2013-12-31 '+
'</Value></Leq></And><And><Geq><FieldRef Name="DueDate" /><Value IncludeTimeValue="TRUE" Type="DateTime"> 2013-11-01 '+
'</Value></Geq><Leq><FieldRef Name="DueDate" /><Value IncludeTimeValue="TRUE" Type="DateTime">’ 2013-12-31’</Value></Leq></And></Or>' +
'<And><Geq><FieldRef Name="DueDate" /><Value IncludeTimeValue="TRUE" Type="DateTime">' 2013-12-31'</Value></Geq><Leq><FieldRef Name="StartDate" /><Value IncludeTimeValue="TRUE" Type="DateTime"> 2013-11-01 '+
'</Value></Leq></And></Or>' +
'</Where></Query>'
![Page 31: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/31.jpg)
Working with SCOM
CAML
There’s a flag on the play
![Page 32: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/32.jpg)
• Simplified Queries
((StartDate ge ‘2013-11-01’ and StartDate le ‘2013-12-31’) or (DueDate ge ‘2013-11-01’ and DueDate le ‘2013-12-31’) or (DueDate ge ' 2013-12-31 ' and StartDate le ‘2013-11-01’))
Working with REST
![Page 33: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/33.jpg)
Touchdown!!
2 Point Conversion is good!!!
Working with REST
![Page 34: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/34.jpg)
Working with CSOM
Easier learning curve for .NET Devs
Similar to Server Object Model
Compilation in non JavaScript implementations
![Page 35: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/35.jpg)
FIELD GOAL FOR CSOM!It’s good!
![Page 36: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/36.jpg)
CSOM REST
13 17
0QTR
3
0 0 0:
![Page 37: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/37.jpg)
F0URTH QUARTERBatch Processing/Performance
![Page 38: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/38.jpg)
CSOM Batch Processing• Batch Updates• Batch Exception Handling
– One call to server to handle try, catch, finally
![Page 39: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/39.jpg)
CSOM Batch Processing
Batch Insert
function CreateListItems(objArray) {
var itemArray = [];
var clientContext = SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('ListName');
for(index in objArray){
var curObject = itemArray[index];
var itemCreateInfo = new SP.ListItemCreationInformation();
var oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title', curObject.title);
oListItem.update();
itemArray[i] = oListItem;
clientContext.load(itemArray[i]);
}
clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
}
![Page 40: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/40.jpg)
CSOM Batch Processing
Batch Exception Handling
scope = new SP.ExceptionHandlingScope(context); var scopeStart = scope.startScope();
var scopeTry = scope.startTry(); list = web.get_lists().getByTitle("Tasks"); context.load(list); scopeTry.dispose();
var scopeCatch = scope.startCatch(); var lci = new SP.ListCreationInformation(); lci.set_title("Tasks"); lci.set_quickLaunchOption(SP.QuickLaunchOptions.on); lci.set_templateType(SP.ListTemplateType.tasks); list = web.get_lists().add(lci); scopeCatch.dispose();
var scopeFinally = scope.startFinally(); list = web.get_lists().getByTitle("Tasks"); context.load(list); scopeFinally.dispose();
scopeStart.dispose();
context.executeQueryAsync(success, fail);
![Page 41: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/41.jpg)
CSOM Batch Processing
TOUCHDOWN!
![Page 42: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/42.jpg)
REST Batch Processing
FUMBLE!
![Page 43: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/43.jpg)
REST vs. CSOM Performance
• REST is “chattier”– No batching
• CSOM returns more data– Bigger packets
• REST can return array of JSON objects– Can feed array directly to libraries without
transformation or iteration
![Page 44: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/44.jpg)
FIELD GOAL FOR REST!!It’s good!
![Page 45: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/45.jpg)
CSOM REST
20 20
0QTR
4
0 0 0:
![Page 46: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/46.jpg)
OVERTIME!Anonymous Access
![Page 47: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/47.jpg)
KIRK EVANS - WHAT EVERY DEVELOPER NEEDS TO KNOW ABOUT SHAREPOINT APPS, CSOM, AND ANONYMOUS PUBLISHING SITES
http://blogs.msdn.com/b/kaevans/archive/2013/10/24/what-every-developer-needs-to-know-about-sharepoint-apps-csom-and-anonymous-publishing-sites.aspx
![Page 48: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/48.jpg)
REST & CSOM Anonymous Access
On-Premises
![Page 49: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/49.jpg)
REST & CSOM Anonymous Access
On-Premises
• According to Microsoft “Not Advisable”– Exposes too much information to nosey people– Opens you up to DoS attacks
• Reality– SPServices can be used by nosey people (can’t turn it
off)– Everyone is open to a DoS attack
![Page 50: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/50.jpg)
REST & CSOM Anonymous Access
Office 365
Anonymous Access with REST* or CSOM is not possible in Office 365
*It Depends
![Page 51: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/51.jpg)
CSOM Anonymous• Blocked by default
– GetItems and GetChanges on SPLists– GetChanges and GetSubwebsForCurrentUser on SPWebs– GetChanges on SPSites
• Use PowerShell to enable$webapp = Get-SPWebApplication "http://WebAppUrl"
$webapp.ClientCallableSettings.AnonymousRestrictedTypes.Remove([microsoft.sharepoint.splist], "GetItems")
$webapp.Update()
![Page 52: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/52.jpg)
REST Anonymous
Search in REST
Waldek Mastykarz - Configuring SharePoint 2013 Search REST API for anonymous users
http://blog.mastykarz.nl/configuring-sharepoint-2013-search-rest-api-anonymous-users/
![Page 53: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/53.jpg)
FIELD GOAL REST!!!!!!!!
REST WINS!
![Page 54: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/54.jpg)
CSOM REST
20 23
0QTR
OT
0 0 0:
![Page 55: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/55.jpg)
Game HighlightsCSOM REST
More API Coverage Than REST
Designed to be similar to .NET Object Model, more comfortable for .NET Devs
Handles batch processing Well
CAML still sucks
Anonymous possible but not advised
![Page 56: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/56.jpg)
Game HighlightsCSOM REST
More API Coverage Than REST NO MORE CAML!
Designed to be similar to .NET Object Model, more comfortable for .NET Devs
Platform independent, REST/oData play well with other libraries
Handles batch processing Well Easy testing in browser using URL
CAML still sucks Can perform better than CSOM
Anonymous possible but not advised
Anonymous search works well, other anonymous possible as well but not advised
![Page 57: SharePoint REST vs CSOM](https://reader038.fdocuments.net/reader038/viewer/2022102506/54c551f44a7959ea038b45cf/html5/thumbnails/57.jpg)
QUESTIONS??There are no stupid ones…