SlideShare a Scribd company logo
1 of 32
Introduction to the SharePoint 2013 Client Object
Model and REST API
Rob Windsor
rob@robwindsor.com
@robwindsor
About Me
 Senior SharePoint Consultant
 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 Object Model (CSOM)
 API used when building remote applications
 Designed to be similar to the Server Object Model
 Introduced in SharePoint 2010, expanded in SharePoint 2013
 Slightly different versions for SP 2013 on-premises and SP Online
 Three implementations
 .NET Managed, Silverlight (plus Mobile), JavaScript
 Façades on top of /_vti_bin/Client.svc
 Managed implementation has two versions
 Version 15 is for use against an on-premises farm
 Version 16 is for use against SharePoint Online
 Communication with SharePoint done in batches
Client Object Model Coverage
 Sites, Webs, Features, Event Receivers
 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
Communicating with SharePoint
 All CRUD operations are automatically batched
 Requests for resources batched using Load and LoadQuery methods
 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
 Response contains requested resources in JSON format
Client Object Model Authentication
 .NET Managed
 Windows credentials passed by default
 ClientContext.AuthenticationMode
 Default
 Anonymous
 FormsAuthentication
 ClientContext.Credentials
 Expects System.Net.ICredentials
 NetworkCredential, SharePointOnlineCredentials, …
 ClientContext.FormsAuthenticationLoginInfo
 Silverlight and JavaScript
 Credentials of the hosting Web application are always used
Retrieving Resources Using Load
var context = new ClientContext(siteUrl)
var web = context.Web;
context.Load(web);
context.Load(web.Lists);
context.ExecuteQuery();
ResultsListBox.Items.Add(web.Title);
ResultsListBox.Items.Add(web.Lists.Count);
 Indicates object data should be included in next batch retrieval
 Not all property values are retrieved
 Example: collections of associated objects
var context = SP.ClientContext.get_current();
var web = context.get_web();
var lists = web.get_lists();
context.load(web);
context.load(lists);
context.executeQueryAsync(success, fail);
function success() {
var div = jQuery("#message");
div.text(web.get_title());
div.append("<br />");
div.append(lists.get_count());
}
Managed: JavaScript:
Retrieving Resources Using LoadQuery
(Managed Code)
var web = context.Web;
var query = from list in web.Lists
where list.Hidden == false &&
list.ItemCount > 0
select list;
var lists = context.LoadQuery(query);
context.ExecuteQuery();
Console.WriteLine(lists.Count());
 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
Retrieving Resources Using loadQuery
(JavaScript)
var context = SP.ClientContext.get_current();
var lists = context.get_web().get_lists();
context.load(lists);
context.executeQueryAsync(success, fail);
function success() {
var div = jQuery("#message");
div.text(lists.get_count());
}
 No LINQ in JavaScript
 loadQuery very similar to load
 Returns new object
 Returns array for collections
var context = SP.ClientContext.get_current();
var lists = context.get_web().get_lists();
var myLists = context.loadQuery(lists);
context.executeQueryAsync(success, fail);
function success() {
var div = jQuery("#message");
div.text(myLists.length);
}
load: loadQuery:
Selecting Fields to Retrieve
var web = context.Web;
context.Load(web, w => w.Title, w => w.Description);
var query = from list in web.Lists.Include(l => l.Title)
where list.Hidden == false &&
list.ItemCount > 0
select list;
var lists = context.LoadQuery(query);
context.ExecuteQuery();
 Limit fields returned to reduce network traffic
 Use parameter array in Load and LoadQuery
 Use Include for collections
var web = context.get_web();
var lists = web.get_lists();
context.load(web, "Title", "Description");
context.load(lists, "Include(Title)");
context.executeQueryAsync(success, fail);
Managed: JavaScript:
Retrieving List Items
 Somewhat different than Server OM
 Set of items accessed by List.GetItems method
 Forces use of CAML query to encourage reduced result sets
 Selecting fields to be returned
 Can use ViewFields in query
 Can use Include with Load or LoadQuery
 CSOM does not support cross-list CAML queries
 Can use KeywordQuery with Search API for similar results
Task Server OM Managed Client OM
Get list web.Lists[“Products”] web.Lists.GetByTitle(“Products”)
Get items list.Items list.GetItems(query)
Get item title item.Title item[“Title”]
Query type SPQuery CamlQuery
Using CAML Queries
var web = context.Web;
var list = web.Lists.GetByTitle("Products");
var query = new CamlQuery();
query.ViewXml = "<View>" +
"<Query>" +
"<Where><Eq>" +
"<FieldRef Name='Category' " +
"LookupId='True' />" +
"<Value Type='Lookup'>1</Value>" +
"</Eq></Where>" +
"</Query>" +
"</View>";
var items = list.GetItems(query);
context.Load(items,
c => c.Include(li => li["ID"], li => li["Title"]));
context.ExecuteQuery();
var context = SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle("Products");
var query = new SP.CamlQuery();
query.set_viewXml("<View>" +
"<Query>" +
"<Where><Eq>" +
"<FieldRef Name='Category' " +
"LookupId='True' />" +
"<Value Type='Lookup'>1</Value>" +
"</Eq></Where>" +
"</Query>" +
"<RowLimit>5</RowLimit>" +
"</View>");
var items = list.getItems(query);
context.load(web, "Title");
context.load(items, "Include(ID, Title)");
context.executeQueryAsync(success, fail);
Managed: JavaScript:
REST API
 API used when building remote applications
 What is the REST API in SharePoint
 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’)
 http://<site url>/_api/web/lists/getByTitle(‘Customers’)/items
 Operations on resources map to HTTP Verbs
 GET, PUT, POST, DELETE, …
 Results from service returned in AtomPub (XML) or JavaScript Object
Notation (JSON) format
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
 Still available for backwards compatibility
 RESTful operations added to /_vti_bin/Client.svc
 /_api added as an alias for /_vti_bin/Client.svc
REST API Coverage
 Sites, Webs, Features, Event Receivers
 Lists, List Items, Fields, Content Types, Views, Forms, IRM
 Files, Folders
 Users, Roles, Groups, User Profiles, Feeds
 Search
 No support for Managed Metadata
 Term Store or Managed Metadata Fields
 No support for Workflow
Retrieving Data (Managed)
 Service metadata for /_api was added around April 2013
 You can add a service reference in Visual Studio
 Tooling to add a service reference for SharePoint Online does not work
 Service proxy contains two context classes
 SP.Data.ListData – access to list data
 SP.ApiData – access to everything else
 Generated proxy classes do not natively support updates
 SharePoint REST API works differently than OData
 Need to use POST tunneling and client hooks to get updates to work
 Too much work – better off using CSOM
 For more detail see white paper by Paul Schaelein
 SharePoint 2013 REST and WCF Data Services
 http://www.schaeflein.net/Pages/SharePoint-2013-REST-and-WCF-
Data-Services.aspx
 Still have the option of using the 2010 version of REST API
Retrieving Data (Managed)
var svcUri = new Uri(siteUrl + "/_api");
var context = new SP2013Proxy.SP.ApiData(svcUri);
context.Credentials = System.Net.CredentialCache.DefaultCredentials;
var resourceUri = new Uri("/web", UriKind.Relative);
var webs = context.Execute<SP2013Proxy.SP.Web>(resourceUri);
var web = webs.First();
ResultsListBox.Items.Add(web.Title);
Retrieving Data (Managed)
 Have option of doing HTTP requests
 Need to work with raw XML or JSON
var url = siteUrl + "/_api/Web/";
var client = new WebClient();
client.UseDefaultCredentials = true;
client.Headers[HttpRequestHeader.Accept] = "application/json;odata=verbose";
var json = client.DownloadString(url);
var ser = new JavaScriptSerializer();
dynamic item = ser.Deserialize<object>(json);
ResultsListBox.Items.Add(item["d"]["Title"]);
Retrieving Data (JavaScript)
 Use jQuery to make service call
 Use _spPageContextInfo to get site URL
 Use Accept header to request JSON response
var call = jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/",
type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
}
});
call.done(function (data, textStatus, jqXHR) {
var div = jQuery("#message");
div.text(data.d.Title);
});
call.fail(function (jqXHR, textStatus, errorThrown) {
alert("Call failed. Error: " + errorThrown);
});
OData Queries
 Queries represented by query strings added to resource URL
Option Example
$select _api/Web/Lists?$select=Title,ItemCount
$filter _api/Web/Lists?$filter=(Hidden eq false)
$orderby _api/Web/Lists?$orderby=ItemCount desc
$skip, $top _api/Web/Lists?$skip=25&$top=10
$expand _api/Web/Lists?$expand=Fields
Full documentation: http://www.odata.org/documentation/odata-v2-documentation/
uri-conventions/#4_Query_String_Options (http://bit.ly/10dqevp)
OData Continuations
 OData provider may limit number of item in response
 Need to check for __next (JSON) or link element (AtomPub)
 Use URL to get next set of results
CAML Queries
 Must be executed using a POST
 Headers must include Form Digest
var viewXml = { ViewXml: "<View>" +
"<Query>" +
"<Where><Eq>" +
"<FieldRef Name='Category' LookupId='True' />" +
"<Value Type='Lookup'>1</Value>" +
"</Eq></Where>" +
"</Query>" +
"</View>"
}
var call = jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl +
"/_api/Web/Lists/getByTitle('Products')/GetItems(query=@v1)?" +
@v1=" + JSON.stringify(viewXml),
type: "POST",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
}
});
Form Digest
 Protects against replay attacks
 Value available in hidden field on SharePoint page
 Unique to user and site
 Only valid for limited time
 Use UpdateFormDigest() function to refresh value in hidden
field
 Service call only make if form digest has expired
 For more details see blog post by Wictor Wilen
 How to refresh the Request Digest value in JavaScript
 http://www.wictorwilen.se/sharepoint-2013-how-to-refresh-the-request-
digest-value-in-javascript
Creating a List (CSOM)
 Moderately different than code for Server Object Model
 Adding the list
 Web.Lists.Add(creationInformation)
 Parameter is type ListCreationInformation
var web = context.Web;
var lci = new ListCreationInformation();
lci.Title = "Tasks";
lci.QuickLaunchOption = QuickLaunchOptions.On;
lci.TemplateType = (int)ListTemplateType.Tasks;
var list = web.Lists.Add(lci);
var web = context.get_web();
var lci = new SP.ListCreationInformation();
lci.set_title("Tasks");
lci.set_quickLaunchOption(SP.QuickLaunchOptions.on);
lci.set_templateType(SP.ListTemplateType.tasks);
var list = web.get_lists().add(lci);
Managed: JavaScript:
Creating a List (REST)
 Send POST to /_api/Web/Lists
 Message body has SP.List object with properties
 Fills same role as SP.ListCreationInformation object in CSOM
 Must include Form Digest in headers
var call = jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists",
type: "POST",
data: JSON.stringify({
"__metadata": { type: "SP.List" },
BaseTemplate: SP.ListTemplateType.tasks,
Title: "Tasks"
}),
headers: {
Accept: "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
}
});
Creating and Updating List Items (CSOM)
 Virtually the same as code for Server Object Model
 Adding a list item
 List.AddItem(creationInformation)
 Parameter is type ListItemCreationInformation
 Updating field values
 Exactly the same as Server Object Model code
var web = context.Web;
var list = web.Lists.GetByTitle("Tasks");
var ici = new ListItemCreationInformation();
var item = list.AddItem(ici);
item["Title"] = "Sample Task";
item["AssignedTo"] = web.CurrentUser;
item["DueDate"] = DateTime.Now.AddDays(7);
item.Update();
var web = context.get_web();
var list = web.get_lists().getByTitle("Tasks");
var ici = new SP.ListItemCreationInformation();
var item = list.addItem(ici);
item.set_item("Title", "Sample Task");
item.set_item("AssignedTo", web.get_currentUser());
var due = new Date();
due.setDate(due.getDate() + 7);
item.set_item("DueDate", due);
item.update();
Managed: JavaScript:
Creating List Items (REST - JavaScript)
 Post to /_api/Web/Lists/getByTitle(‘<List Name>’)/Items
 Type name is SP.Data.<List Name>ListItem
var call = jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('Tasks')/Items",
type: "POST",
data: JSON.stringify({
"__metadata": { type: "SP.Data.TasksListItem" },
Title: "Sample Task",
AssignedToId: userId,
DueDate: due
}),
headers: {
Accept: "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
}
});
Creating List Items (REST - Managed)
 Create instance of list item type
 One of the generated types in the service proxy
 Set property values
 Add to list using the AddTo<list name> method on context
var svcUri = new Uri(siteUrl + "/_vti_bin/ListData.svc");
var context = new SP2010Proxy.DemoDataContext(svcUri);
context.Credentials = System.Net.CredentialCache.DefaultCredentials;
var item = new SP2010Proxy.ProductsItem();
item.Title = "Test Product";
item.ProductID = 999;
item.CategoryId = 1;
item.UnitPrice = 9.99;
item.UnitsInStock = 99;
context.AddToProducts(item);
context.SaveChanges();
Updating List Items (REST - Managed)
 Get list item
 Update property values
 Call UpdateObject on context
var svcUri = new Uri(siteUrl + "/_vti_bin/ListData.svc");
var context = new SP2010Proxy.DemoDataContext(svcUri);
context.Credentials = System.Net.CredentialCache.DefaultCredentials;
var query = from product in context.Products
where product.ProductID == 999
select product;
var item = query.FirstOrDefault();
if (item != null)
{
item.UnitPrice = 4.44;
item.UnitsInStock = 44;
context.UpdateObject(item);
context.SaveChanges();
}
Updating List Items (REST - JavaScript)
 Send to /_api/Web/Lists/getByTitle(‘<List>')/Items(<Item Id>)
 Request type (X-Http-Method)
 Can update by sending PUT
 All writable field values must be specified
 Can update by sending POST
 Set X-Http-Method to PATCH or MERGE
 Only send field values that are changing
 Concurrency (IF-MATCH)
 Item metadata includes etag which represents the version
 Set IF-MATCH in header to etag value
 Update will fail if item has been updated since read
 SET IF-MATCH in header to *
 Update will overwrite changes (if any)
Updating List Items (REST - JavaScript)
var call = jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl +
"/_api/Web/Lists/getByTitle('Tasks')/Items(" + item.Id + ")",
type: "POST",
data: JSON.stringify({
"__metadata": { type: "SP.Data.TasksListItem" },
Status: "In Progress",
PercentComplete: 0.10
}),
headers: {
Accept: "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
"IF-MATCH": item.__metadata.etag,
"X-Http-Method": "PATCH"
}
});
Thank You
 Big thanks to the organizers, sponsors and you for
making this event possible
 Please fill out your evaluation
 Please keep in touch
rob@robwindsor.com
@robwindsor
blogs.msmvps.com/windsor

More Related Content

What's hot

Monads do not Compose
Monads do not ComposeMonads do not Compose
Monads do not Compose
Philip Schwarz
 
Advanced Sql Injection ENG
Advanced Sql Injection ENGAdvanced Sql Injection ENG
Advanced Sql Injection ENG
Dmitry Evteev
 
좌충우돌 ORM 개발기 | Devon 2012
좌충우돌 ORM 개발기 | Devon 2012좌충우돌 ORM 개발기 | Devon 2012
좌충우돌 ORM 개발기 | Devon 2012
Daum DNA
 
Simplified Security Code Review Process
Simplified Security Code Review ProcessSimplified Security Code Review Process
Simplified Security Code Review Process
Sherif Koussa
 

What's hot (20)

Hack an ASP .NET website? Hard, but possible!
Hack an ASP .NET website? Hard, but possible! Hack an ASP .NET website? Hard, but possible!
Hack an ASP .NET website? Hard, but possible!
 
Hibernate
HibernateHibernate
Hibernate
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Pokemon battle simulator (Java Program written on Blue J Editor)
Pokemon battle simulator (Java Program written on Blue J Editor)Pokemon battle simulator (Java Program written on Blue J Editor)
Pokemon battle simulator (Java Program written on Blue J Editor)
 
Monads do not Compose
Monads do not ComposeMonads do not Compose
Monads do not Compose
 
Comprehensive overview FAPI 1 and FAPI 2
Comprehensive overview FAPI 1 and FAPI 2Comprehensive overview FAPI 1 and FAPI 2
Comprehensive overview FAPI 1 and FAPI 2
 
Sqlmap
SqlmapSqlmap
Sqlmap
 
Advanced Sql Injection ENG
Advanced Sql Injection ENGAdvanced Sql Injection ENG
Advanced Sql Injection ENG
 
Sql injection
Sql injectionSql injection
Sql injection
 
Best Practices for Password Creation
Best Practices for Password CreationBest Practices for Password Creation
Best Practices for Password Creation
 
Sql Injection and XSS
Sql Injection and XSSSql Injection and XSS
Sql Injection and XSS
 
Railway Oriented Programming
Railway Oriented ProgrammingRailway Oriented Programming
Railway Oriented Programming
 
How to identify and prevent SQL injection
How to identify and prevent SQL injection  How to identify and prevent SQL injection
How to identify and prevent SQL injection
 
좌충우돌 ORM 개발기 | Devon 2012
좌충우돌 ORM 개발기 | Devon 2012좌충우돌 ORM 개발기 | Devon 2012
좌충우돌 ORM 개발기 | Devon 2012
 
Sql injections - with example
Sql injections - with exampleSql injections - with example
Sql injections - with example
 
Restful api
Restful apiRestful api
Restful api
 
Workshop-Demo Breakdown.pptx
Workshop-Demo Breakdown.pptxWorkshop-Demo Breakdown.pptx
Workshop-Demo Breakdown.pptx
 
Simplified Security Code Review Process
Simplified Security Code Review ProcessSimplified Security Code Review Process
Simplified Security Code Review Process
 
Spring Core
Spring CoreSpring Core
Spring Core
 
XSS & SQL injection
XSS & SQL injectionXSS & SQL injection
XSS & SQL injection
 

Viewers also liked

Integrating SharePoint 2010 and Visual Studio Lightswitch
Integrating SharePoint 2010 and Visual Studio LightswitchIntegrating SharePoint 2010 and Visual Studio Lightswitch
Integrating SharePoint 2010 and Visual Studio Lightswitch
Rob Windsor
 
Understanding SharePoint Content Types
Understanding SharePoint Content TypesUnderstanding SharePoint Content Types
Understanding SharePoint Content Types
Benjamin Niaulin
 
SharePoint Online vs. On-Premise
SharePoint Online vs. On-PremiseSharePoint Online vs. On-Premise
SharePoint Online vs. On-Premise
Evan Hodges
 

Viewers also liked (19)

Integrating SharePoint 2010 and Visual Studio Lightswitch
Integrating SharePoint 2010 and Visual Studio LightswitchIntegrating SharePoint 2010 and Visual Studio Lightswitch
Integrating SharePoint 2010 and Visual Studio Lightswitch
 
SP24 S055 SharePointToolbox by Rodrigo Pinto
SP24 S055 SharePointToolbox by Rodrigo PintoSP24 S055 SharePointToolbox by Rodrigo Pinto
SP24 S055 SharePointToolbox by Rodrigo Pinto
 
Office 365 Deployment Strategies
Office 365 Deployment StrategiesOffice 365 Deployment Strategies
Office 365 Deployment Strategies
 
Customizing SharePoint Online
Customizing SharePoint OnlineCustomizing SharePoint Online
Customizing SharePoint Online
 
Understanding SharePoint Content Types
Understanding SharePoint Content TypesUnderstanding SharePoint Content Types
Understanding SharePoint Content Types
 
Stop Updating, Start Evolving - The Digital Workplace Truth
Stop Updating, Start Evolving - The Digital Workplace TruthStop Updating, Start Evolving - The Digital Workplace Truth
Stop Updating, Start Evolving - The Digital Workplace Truth
 
Understand the SharePoint Basics
Understand the SharePoint BasicsUnderstand the SharePoint Basics
Understand the SharePoint Basics
 
Understanding and programming the SharePoint REST API
Understanding and programming the SharePoint REST APIUnderstanding and programming the SharePoint REST API
Understanding and programming the SharePoint REST API
 
SharePoint Online vs. On-Premise
SharePoint Online vs. On-PremiseSharePoint Online vs. On-Premise
SharePoint Online vs. On-Premise
 
Build Killer Visuals with SharePoint 2013 Search & Display Templates
Build Killer Visuals with SharePoint 2013 Search & Display TemplatesBuild Killer Visuals with SharePoint 2013 Search & Display Templates
Build Killer Visuals with SharePoint 2013 Search & Display Templates
 
Office 365 deployment fast track
Office 365 deployment fast trackOffice 365 deployment fast track
Office 365 deployment fast track
 
Discover SharePoint 2016 Preview and the Vision
Discover SharePoint 2016 Preview and the VisionDiscover SharePoint 2016 Preview and the Vision
Discover SharePoint 2016 Preview and the Vision
 
Understanding SharePoint site structure what's inside
Understanding SharePoint site structure  what's insideUnderstanding SharePoint site structure  what's inside
Understanding SharePoint site structure what's inside
 
App Model For SharePoint 2013
App Model For SharePoint 2013App Model For SharePoint 2013
App Model For SharePoint 2013
 
SharePoint Online v Onprem
SharePoint Online v OnpremSharePoint Online v Onprem
SharePoint Online v Onprem
 
What and how do I choose SharePoint 2013 On-premise vs. Cloud (Office 365)
What and how do I choose SharePoint 2013 On-premise vs. Cloud (Office 365)What and how do I choose SharePoint 2013 On-premise vs. Cloud (Office 365)
What and how do I choose SharePoint 2013 On-premise vs. Cloud (Office 365)
 
SharePoint 2016: Features Overview
SharePoint 2016: Features OverviewSharePoint 2016: Features Overview
SharePoint 2016: Features Overview
 
Comprendre la recherche dans SharePoint
Comprendre la recherche dans SharePointComprendre la recherche dans SharePoint
Comprendre la recherche dans SharePoint
 
Mieux comprendre SharePoint 2013
Mieux comprendre SharePoint 2013Mieux comprendre SharePoint 2013
Mieux comprendre SharePoint 2013
 

Similar to Introduction to the SharePoint Client Object Model and REST API

Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...
Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...
Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...
SharePoint Saturday NY
 
Wss Object Model
Wss Object ModelWss Object Model
Wss Object Model
maddinapudi
 
Taking Advantage of the SharePoint 2013 REST API
Taking Advantage of the SharePoint 2013 REST APITaking Advantage of the SharePoint 2013 REST API
Taking Advantage of the SharePoint 2013 REST API
Eric Shupps
 
Web Technologies - forms and actions
Web Technologies -  forms and actionsWeb Technologies -  forms and actions
Web Technologies - forms and actions
Aren Zomorodian
 
[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기
[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기
[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기
lanslote
 
jsSaturday - PhoneGap and jQuery Mobile for SharePoint 2013
jsSaturday - PhoneGap and jQuery Mobile for SharePoint 2013jsSaturday - PhoneGap and jQuery Mobile for SharePoint 2013
jsSaturday - PhoneGap and jQuery Mobile for SharePoint 2013
Kiril Iliev
 
Data Access Options in SharePoint 2010
Data Access Options in SharePoint 2010Data Access Options in SharePoint 2010
Data Access Options in SharePoint 2010
Rob Windsor
 

Similar to Introduction to the SharePoint Client Object Model and REST API (20)

Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...
Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...
Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...
 
Wss Object Model
Wss Object ModelWss Object Model
Wss Object Model
 
Building RESTful applications using Spring MVC
Building RESTful applications using Spring MVCBuilding RESTful applications using Spring MVC
Building RESTful applications using Spring MVC
 
Working with a super model for SharePoint Tuga IT 2016
Working with a super model for SharePoint Tuga IT 2016Working with a super model for SharePoint Tuga IT 2016
Working with a super model for SharePoint Tuga IT 2016
 
Taking Advantage of the SharePoint 2013 REST API
Taking Advantage of the SharePoint 2013 REST APITaking Advantage of the SharePoint 2013 REST API
Taking Advantage of the SharePoint 2013 REST API
 
Web Technologies - forms and actions
Web Technologies -  forms and actionsWeb Technologies -  forms and actions
Web Technologies - forms and actions
 
[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기
[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기
[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기
 
Share point hosted add ins munich
Share point hosted add ins munichShare point hosted add ins munich
Share point hosted add ins munich
 
jsSaturday - PhoneGap and jQuery Mobile for SharePoint 2013
jsSaturday - PhoneGap and jQuery Mobile for SharePoint 2013jsSaturday - PhoneGap and jQuery Mobile for SharePoint 2013
jsSaturday - PhoneGap and jQuery Mobile for SharePoint 2013
 
Developing With Data Technologies
Developing With Data TechnologiesDeveloping With Data Technologies
Developing With Data Technologies
 
Rest API and Client OM for Developer
Rest API and Client OM for DeveloperRest API and Client OM for Developer
Rest API and Client OM for Developer
 
Introduction to the SharePoint 2013 REST API
Introduction to the SharePoint 2013 REST APIIntroduction to the SharePoint 2013 REST API
Introduction to the SharePoint 2013 REST API
 
SharePoint Object Model, Web Services and Events
SharePoint Object Model, Web Services and EventsSharePoint Object Model, Web Services and Events
SharePoint Object Model, Web Services and Events
 
Node.js and Parse
Node.js and ParseNode.js and Parse
Node.js and Parse
 
Data Access Options in SharePoint 2010
Data Access Options in SharePoint 2010Data Access Options in SharePoint 2010
Data Access Options in SharePoint 2010
 
ADO.NET Data Services
ADO.NET Data ServicesADO.NET Data Services
ADO.NET Data Services
 
Tutorial, Part 2: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
Tutorial, Part 2: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...Tutorial, Part 2: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
Tutorial, Part 2: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
 
How To Manage API Request with AXIOS on a React Native App
How To Manage API Request with AXIOS on a React Native AppHow To Manage API Request with AXIOS on a React Native App
How To Manage API Request with AXIOS on a React Native App
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web services
 
Charla desarrollo de apps con sharepoint y office 365
Charla   desarrollo de apps con sharepoint y office 365Charla   desarrollo de apps con sharepoint y office 365
Charla desarrollo de apps con sharepoint y office 365
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

Introduction to the SharePoint Client Object Model and REST API

  • 1. Introduction to the SharePoint 2013 Client Object Model and REST API Rob Windsor rob@robwindsor.com @robwindsor
  • 2. About Me  Senior SharePoint Consultant  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)
  • 3. Client Object Model (CSOM)  API used when building remote applications  Designed to be similar to the Server Object Model  Introduced in SharePoint 2010, expanded in SharePoint 2013  Slightly different versions for SP 2013 on-premises and SP Online  Three implementations  .NET Managed, Silverlight (plus Mobile), JavaScript  Façades on top of /_vti_bin/Client.svc  Managed implementation has two versions  Version 15 is for use against an on-premises farm  Version 16 is for use against SharePoint Online  Communication with SharePoint done in batches
  • 4. Client Object Model Coverage  Sites, Webs, Features, Event Receivers  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
  • 5. Communicating with SharePoint  All CRUD operations are automatically batched  Requests for resources batched using Load and LoadQuery methods  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  Response contains requested resources in JSON format
  • 6. Client Object Model Authentication  .NET Managed  Windows credentials passed by default  ClientContext.AuthenticationMode  Default  Anonymous  FormsAuthentication  ClientContext.Credentials  Expects System.Net.ICredentials  NetworkCredential, SharePointOnlineCredentials, …  ClientContext.FormsAuthenticationLoginInfo  Silverlight and JavaScript  Credentials of the hosting Web application are always used
  • 7. Retrieving Resources Using Load var context = new ClientContext(siteUrl) var web = context.Web; context.Load(web); context.Load(web.Lists); context.ExecuteQuery(); ResultsListBox.Items.Add(web.Title); ResultsListBox.Items.Add(web.Lists.Count);  Indicates object data should be included in next batch retrieval  Not all property values are retrieved  Example: collections of associated objects var context = SP.ClientContext.get_current(); var web = context.get_web(); var lists = web.get_lists(); context.load(web); context.load(lists); context.executeQueryAsync(success, fail); function success() { var div = jQuery("#message"); div.text(web.get_title()); div.append("<br />"); div.append(lists.get_count()); } Managed: JavaScript:
  • 8. Retrieving Resources Using LoadQuery (Managed Code) var web = context.Web; var query = from list in web.Lists where list.Hidden == false && list.ItemCount > 0 select list; var lists = context.LoadQuery(query); context.ExecuteQuery(); Console.WriteLine(lists.Count());  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
  • 9. Retrieving Resources Using loadQuery (JavaScript) var context = SP.ClientContext.get_current(); var lists = context.get_web().get_lists(); context.load(lists); context.executeQueryAsync(success, fail); function success() { var div = jQuery("#message"); div.text(lists.get_count()); }  No LINQ in JavaScript  loadQuery very similar to load  Returns new object  Returns array for collections var context = SP.ClientContext.get_current(); var lists = context.get_web().get_lists(); var myLists = context.loadQuery(lists); context.executeQueryAsync(success, fail); function success() { var div = jQuery("#message"); div.text(myLists.length); } load: loadQuery:
  • 10. Selecting Fields to Retrieve var web = context.Web; context.Load(web, w => w.Title, w => w.Description); var query = from list in web.Lists.Include(l => l.Title) where list.Hidden == false && list.ItemCount > 0 select list; var lists = context.LoadQuery(query); context.ExecuteQuery();  Limit fields returned to reduce network traffic  Use parameter array in Load and LoadQuery  Use Include for collections var web = context.get_web(); var lists = web.get_lists(); context.load(web, "Title", "Description"); context.load(lists, "Include(Title)"); context.executeQueryAsync(success, fail); Managed: JavaScript:
  • 11. Retrieving List Items  Somewhat different than Server OM  Set of items accessed by List.GetItems method  Forces use of CAML query to encourage reduced result sets  Selecting fields to be returned  Can use ViewFields in query  Can use Include with Load or LoadQuery  CSOM does not support cross-list CAML queries  Can use KeywordQuery with Search API for similar results Task Server OM Managed Client OM Get list web.Lists[“Products”] web.Lists.GetByTitle(“Products”) Get items list.Items list.GetItems(query) Get item title item.Title item[“Title”] Query type SPQuery CamlQuery
  • 12. Using CAML Queries var web = context.Web; var list = web.Lists.GetByTitle("Products"); var query = new CamlQuery(); query.ViewXml = "<View>" + "<Query>" + "<Where><Eq>" + "<FieldRef Name='Category' " + "LookupId='True' />" + "<Value Type='Lookup'>1</Value>" + "</Eq></Where>" + "</Query>" + "</View>"; var items = list.GetItems(query); context.Load(items, c => c.Include(li => li["ID"], li => li["Title"])); context.ExecuteQuery(); var context = SP.ClientContext.get_current(); var web = context.get_web(); var list = web.get_lists().getByTitle("Products"); var query = new SP.CamlQuery(); query.set_viewXml("<View>" + "<Query>" + "<Where><Eq>" + "<FieldRef Name='Category' " + "LookupId='True' />" + "<Value Type='Lookup'>1</Value>" + "</Eq></Where>" + "</Query>" + "<RowLimit>5</RowLimit>" + "</View>"); var items = list.getItems(query); context.load(web, "Title"); context.load(items, "Include(ID, Title)"); context.executeQueryAsync(success, fail); Managed: JavaScript:
  • 13. REST API  API used when building remote applications  What is the REST API in SharePoint  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’)  http://<site url>/_api/web/lists/getByTitle(‘Customers’)/items  Operations on resources map to HTTP Verbs  GET, PUT, POST, DELETE, …  Results from service returned in AtomPub (XML) or JavaScript Object Notation (JSON) format
  • 14. 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  Still available for backwards compatibility  RESTful operations added to /_vti_bin/Client.svc  /_api added as an alias for /_vti_bin/Client.svc
  • 15. REST API Coverage  Sites, Webs, Features, Event Receivers  Lists, List Items, Fields, Content Types, Views, Forms, IRM  Files, Folders  Users, Roles, Groups, User Profiles, Feeds  Search  No support for Managed Metadata  Term Store or Managed Metadata Fields  No support for Workflow
  • 16. Retrieving Data (Managed)  Service metadata for /_api was added around April 2013  You can add a service reference in Visual Studio  Tooling to add a service reference for SharePoint Online does not work  Service proxy contains two context classes  SP.Data.ListData – access to list data  SP.ApiData – access to everything else  Generated proxy classes do not natively support updates  SharePoint REST API works differently than OData  Need to use POST tunneling and client hooks to get updates to work  Too much work – better off using CSOM  For more detail see white paper by Paul Schaelein  SharePoint 2013 REST and WCF Data Services  http://www.schaeflein.net/Pages/SharePoint-2013-REST-and-WCF- Data-Services.aspx  Still have the option of using the 2010 version of REST API
  • 17. Retrieving Data (Managed) var svcUri = new Uri(siteUrl + "/_api"); var context = new SP2013Proxy.SP.ApiData(svcUri); context.Credentials = System.Net.CredentialCache.DefaultCredentials; var resourceUri = new Uri("/web", UriKind.Relative); var webs = context.Execute<SP2013Proxy.SP.Web>(resourceUri); var web = webs.First(); ResultsListBox.Items.Add(web.Title);
  • 18. Retrieving Data (Managed)  Have option of doing HTTP requests  Need to work with raw XML or JSON var url = siteUrl + "/_api/Web/"; var client = new WebClient(); client.UseDefaultCredentials = true; client.Headers[HttpRequestHeader.Accept] = "application/json;odata=verbose"; var json = client.DownloadString(url); var ser = new JavaScriptSerializer(); dynamic item = ser.Deserialize<object>(json); ResultsListBox.Items.Add(item["d"]["Title"]);
  • 19. Retrieving Data (JavaScript)  Use jQuery to make service call  Use _spPageContextInfo to get site URL  Use Accept header to request JSON response var call = jQuery.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/", type: "GET", dataType: "json", headers: { Accept: "application/json;odata=verbose" } }); call.done(function (data, textStatus, jqXHR) { var div = jQuery("#message"); div.text(data.d.Title); }); call.fail(function (jqXHR, textStatus, errorThrown) { alert("Call failed. Error: " + errorThrown); });
  • 20. OData Queries  Queries represented by query strings added to resource URL Option Example $select _api/Web/Lists?$select=Title,ItemCount $filter _api/Web/Lists?$filter=(Hidden eq false) $orderby _api/Web/Lists?$orderby=ItemCount desc $skip, $top _api/Web/Lists?$skip=25&$top=10 $expand _api/Web/Lists?$expand=Fields Full documentation: http://www.odata.org/documentation/odata-v2-documentation/ uri-conventions/#4_Query_String_Options (http://bit.ly/10dqevp)
  • 21. OData Continuations  OData provider may limit number of item in response  Need to check for __next (JSON) or link element (AtomPub)  Use URL to get next set of results
  • 22. CAML Queries  Must be executed using a POST  Headers must include Form Digest var viewXml = { ViewXml: "<View>" + "<Query>" + "<Where><Eq>" + "<FieldRef Name='Category' LookupId='True' />" + "<Value Type='Lookup'>1</Value>" + "</Eq></Where>" + "</Query>" + "</View>" } var call = jQuery.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('Products')/GetItems(query=@v1)?" + @v1=" + JSON.stringify(viewXml), type: "POST", dataType: "json", headers: { Accept: "application/json;odata=verbose", "X-RequestDigest": jQuery("#__REQUESTDIGEST").val() } });
  • 23. Form Digest  Protects against replay attacks  Value available in hidden field on SharePoint page  Unique to user and site  Only valid for limited time  Use UpdateFormDigest() function to refresh value in hidden field  Service call only make if form digest has expired  For more details see blog post by Wictor Wilen  How to refresh the Request Digest value in JavaScript  http://www.wictorwilen.se/sharepoint-2013-how-to-refresh-the-request- digest-value-in-javascript
  • 24. Creating a List (CSOM)  Moderately different than code for Server Object Model  Adding the list  Web.Lists.Add(creationInformation)  Parameter is type ListCreationInformation var web = context.Web; var lci = new ListCreationInformation(); lci.Title = "Tasks"; lci.QuickLaunchOption = QuickLaunchOptions.On; lci.TemplateType = (int)ListTemplateType.Tasks; var list = web.Lists.Add(lci); var web = context.get_web(); var lci = new SP.ListCreationInformation(); lci.set_title("Tasks"); lci.set_quickLaunchOption(SP.QuickLaunchOptions.on); lci.set_templateType(SP.ListTemplateType.tasks); var list = web.get_lists().add(lci); Managed: JavaScript:
  • 25. Creating a List (REST)  Send POST to /_api/Web/Lists  Message body has SP.List object with properties  Fills same role as SP.ListCreationInformation object in CSOM  Must include Form Digest in headers var call = jQuery.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists", type: "POST", data: JSON.stringify({ "__metadata": { type: "SP.List" }, BaseTemplate: SP.ListTemplateType.tasks, Title: "Tasks" }), headers: { Accept: "application/json;odata=verbose", "Content-Type": "application/json;odata=verbose", "X-RequestDigest": jQuery("#__REQUESTDIGEST").val() } });
  • 26. Creating and Updating List Items (CSOM)  Virtually the same as code for Server Object Model  Adding a list item  List.AddItem(creationInformation)  Parameter is type ListItemCreationInformation  Updating field values  Exactly the same as Server Object Model code var web = context.Web; var list = web.Lists.GetByTitle("Tasks"); var ici = new ListItemCreationInformation(); var item = list.AddItem(ici); item["Title"] = "Sample Task"; item["AssignedTo"] = web.CurrentUser; item["DueDate"] = DateTime.Now.AddDays(7); item.Update(); var web = context.get_web(); var list = web.get_lists().getByTitle("Tasks"); var ici = new SP.ListItemCreationInformation(); var item = list.addItem(ici); item.set_item("Title", "Sample Task"); item.set_item("AssignedTo", web.get_currentUser()); var due = new Date(); due.setDate(due.getDate() + 7); item.set_item("DueDate", due); item.update(); Managed: JavaScript:
  • 27. Creating List Items (REST - JavaScript)  Post to /_api/Web/Lists/getByTitle(‘<List Name>’)/Items  Type name is SP.Data.<List Name>ListItem var call = jQuery.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('Tasks')/Items", type: "POST", data: JSON.stringify({ "__metadata": { type: "SP.Data.TasksListItem" }, Title: "Sample Task", AssignedToId: userId, DueDate: due }), headers: { Accept: "application/json;odata=verbose", "Content-Type": "application/json;odata=verbose", "X-RequestDigest": jQuery("#__REQUESTDIGEST").val() } });
  • 28. Creating List Items (REST - Managed)  Create instance of list item type  One of the generated types in the service proxy  Set property values  Add to list using the AddTo<list name> method on context var svcUri = new Uri(siteUrl + "/_vti_bin/ListData.svc"); var context = new SP2010Proxy.DemoDataContext(svcUri); context.Credentials = System.Net.CredentialCache.DefaultCredentials; var item = new SP2010Proxy.ProductsItem(); item.Title = "Test Product"; item.ProductID = 999; item.CategoryId = 1; item.UnitPrice = 9.99; item.UnitsInStock = 99; context.AddToProducts(item); context.SaveChanges();
  • 29. Updating List Items (REST - Managed)  Get list item  Update property values  Call UpdateObject on context var svcUri = new Uri(siteUrl + "/_vti_bin/ListData.svc"); var context = new SP2010Proxy.DemoDataContext(svcUri); context.Credentials = System.Net.CredentialCache.DefaultCredentials; var query = from product in context.Products where product.ProductID == 999 select product; var item = query.FirstOrDefault(); if (item != null) { item.UnitPrice = 4.44; item.UnitsInStock = 44; context.UpdateObject(item); context.SaveChanges(); }
  • 30. Updating List Items (REST - JavaScript)  Send to /_api/Web/Lists/getByTitle(‘<List>')/Items(<Item Id>)  Request type (X-Http-Method)  Can update by sending PUT  All writable field values must be specified  Can update by sending POST  Set X-Http-Method to PATCH or MERGE  Only send field values that are changing  Concurrency (IF-MATCH)  Item metadata includes etag which represents the version  Set IF-MATCH in header to etag value  Update will fail if item has been updated since read  SET IF-MATCH in header to *  Update will overwrite changes (if any)
  • 31. Updating List Items (REST - JavaScript) var call = jQuery.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('Tasks')/Items(" + item.Id + ")", type: "POST", data: JSON.stringify({ "__metadata": { type: "SP.Data.TasksListItem" }, Status: "In Progress", PercentComplete: 0.10 }), headers: { Accept: "application/json;odata=verbose", "Content-Type": "application/json;odata=verbose", "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(), "IF-MATCH": item.__metadata.etag, "X-Http-Method": "PATCH" } });
  • 32. Thank You  Big thanks to the organizers, sponsors and you for making this event possible  Please fill out your evaluation  Please keep in touch rob@robwindsor.com @robwindsor blogs.msmvps.com/windsor

Editor's Notes

  1. 2