Direct access to client.svc not supported
Calls to client.svc must go through supported entry points
Supported entry points:
Silverlight
JavaScript
.NET
In Sharepoint 2013 Client.svc is extented to REST(HTTP GET, PUT, POST requests) API also.
that means Client.svc is accepts HTTP GET, PUT, POST requests
OData Protocal is also implemented.
Sharepoint 2013 CSOM Support following server side functionality.
User Profiles
Search
Taxonomy
Feeds
Publishing
Sharing
Workflow
E-Discovery
IRM
Analytics
Business Data
Sharepoint 2010 CSOM diagram:
Sharepoint 2013 CSOM Diagram:
ListData.svc added REST support for reading & writing to SharePoint lists in SharePoint 2010
it is still present in SP 2013 for backward capability.
SP 2013 new development is done through REST or OData API.
In Sharepoint 2010 CSOM Development following DLL we need to add
Microsoft.Sharepoint.Client.dll
Microsoft.Sharepoint.Client.Runtime.dll
in Sharepoint 2013 CSOM Development( REST is new features).
Microsoft.Sharepoint.Client.dll
Microsoft.Sharepoint.Client.Runtime.dll
Microsoft.Sharepoint.Client.DocumentManagement.dll
Microsoft.Sharepoint.Client.Publishing.dll
Microsoft.Sharepoint.Client.Taxonomy.dll
Microsoft.Sharepoint.Client.UserProfiles.dll
Example :
ClientContext SSite = new ClientContext("http://localhost");
SSite.Credentials = CredentialCache.DefaultCredentials;
Web site = SSite.Web;
ListCollection lists = site.Lists;
// load site info
SSite.Load(site, SiteTitle => SiteTitle.Title);
SSite.ExecuteQuery();
Console.WriteLine("Site Title: " + site.Title);
// create list
ListCreationInformation newList = new ListCreationInformation();
newList.Title = "Clients";
newList.Url = "Lists/Clients";
newList.QuickLaunchOption = QuickLaunchOptions.On;
newList.TemplateType = (int)ListTemplateType.Contacts;
site.Lists.Add(newList);
// lists collection
SiteTitle.Load(lists);
SiteTitle.ExecuteQuery();
foreach (List list in lists)
{
Console.WriteLine(list.Title);
}
CSOM Using Java script :
var ctx;
var web;
var lists;
$(onPageLoad);
function onPageLoad() {
ExecuteOrDelayUntilScriptLoaded(LoadSiteCSOM, "sp.js");
}
function LoadSiteCSOM() {
// here we can't pass site URL ,due to cross site scripting.
ctx = SP.ClientContext.get_current();
web = ctx.get_web();
ctx.load(web);
ctx.load(web.get_currentUser());
lists = web.get_lists();
ctx.load(lists);
ctx.executeQueryAsync(onDisplaySiteInfo, onError);
}
function onDisplaySiteInfo() {
var siteTitle = web.get_title();
var siteId = web.get_id().toString();
var siteUrl = web.get_url();
var currentUser = web.get_currentUser().get_loginName();
}
function onError(sender, args) { alert(JSON.stringify(args)); }
CSOM URL is Changes in with respective Sp2010 and Sp2013
SP 2010 : http://localhost/_vti_bin/client.svc/web
sp 2013 : http://localhost/_api/web
SharePoint 2013 REST and OData fundamentals
OData URIs :
URI has three significant parts – service root URI up to entry point, resource path for SharePoint list or SQL Server table, Query string parameters
Service URL –http://services.odata.org/odata/odata.svc, Resource – category(1)/products, and Query Option – $top=2&$orderby=name
- CSOM URLs can go through _api folder
- _api = _vti_bin/client.svc
- Simplified the URLs that needs to be built to query REST based data – it has less noise, lot easier to use
- Removes client.svc file name from the URL
- Instead of using http://contososerver/_vti_bin/client.svc/web, you can usehttp://contososerver/_api/web for REST based calls, Both options are valid but _api is preferred option
- Mapping Objects to Resources – Example of REST URLs targeting SharePoint Objects
- Access Lists collection from web – _api/web/lists
- Access List 1 from web – _api/web/lists/List1
- Access List 2 from web – _api/web/lists/List2
- Access List from web by GUID – _api/web/lists(guid)
- Access List from web by Title – _api/web/lists/getByTitle(‘Annoucements’)
- URI Examples – Executing REST queries through the browser
- Returns all the information about the site – http://contososerver/_api/web
- Returns title and ID of all the components in site –http://contososerver/_api/web/?$select=title,id
- Returns all the lists in web – http://contososerver/_api/web/lists
- Returns title column of all the lists in web –http://contososerver/_api/web/lists/?$select=title
- Returns information about customer list in web –http://contososerver/_api/web/lists/getbyTitle(‘Customers’)
- Returns all the list items with all the columns from customer list in web –http://contososerver/_api/web/lists/getbyTitle(‘Customers’)/Items
- Returns all the items with only title and firstname column from the customer lists in web –http://contososerver/_api/web/lists/getbyTitle(‘Customers’)/Items/?$select=title,firstname
- Returns only the items title starts with letter P & title and firstname column from the customer lists in web –http://contososerver/_api/web/lists/getbyTitle(‘Customers’)/Items/?$select=title,firstname&startswith(Title,’P’)
Making REST calls with C# and JavaScript for SharePoint 2013
- Returning ATOM XML vs JSON
- SharePoint returns REST based data in two formats – ATOM XML and JSON
- ATOM XML is preferred format in .NET Managed code and JSON is preferred format in JavaScript
- To get ATOM XML, response use MIME type “application/atom+xml”, This is ideal for .NET managed code, you can also use in JavaScript but use XSLT in conjunction with parsing XML to format it properly
- To get JSON, response use MIME type “application/json”, ideal in JavaScript in conjunction with jQuery
- Response data format selected in ACCEPT header, ATOM XML is default option for data format returned by REST based calls in SharePoint, for JSON response, you must pass “application/json” MIME type in ACCEPT header
- REST Query from Managed code – Tips for making REST calls from managed code
- in .NET 4.0 or later, use the HTTPWebRequest and HTTPWebResponse objects instead of WebRequest or WebResponse classes which would allow you to specify Accept property for required MIME string
- Use LINQ-to-XML to get data from XML in response body by loading XML response stream in XDocument object and using XDocument.Descendants method to query data
- REST Query using JavaScript and jQuery
- Use #.getJSON to request the JSON response if you are using jQuery
- Updating using REST based API and Form Digest
- Updates using REST requires Form Digest to protect against replay attack
- Form Digest is available on the SharePoint since MOSS 2007 days, Form Digest is a special value created using cryptography, SharePoint Pages have control holding Form Digest
- The Form Digest control does this by using cryptography to create a special value that is passed to the client browser. This value must be passed back to SharePoint and validated on requests that update objects or content within a SharePoint site
- Add X-RequestDigest header which includes value of form digest
- In the Managed code, form digest can be acquired through _vti_bin/sites.asmxand pass it to the REST based call,
- In the JavaScript, assign the value from the control – $(“#_REQUESTDIGEST).val()
XML Syntax:
ACCEPT = application/atom+xml
JSON Syntax:
ACCEPT = application/json;odata=verbose
REST calls from Managed code :
// build request
Uri uri = new Uri(siteUrl + "/_api/web/lists/getByTitle('Documents')/?&select=Title");
HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
request.Credentials = CredentialCache.DefaultCredentials;
request.Accept = "application/atom+xml";
Uri uri = new Uri(siteUrl + "/_api/web/lists/getByTitle('Documents')/?&select=Title");
HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
request.Credentials = CredentialCache.DefaultCredentials;
request.Accept = "application/atom+xml";
// send request
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
// use LINQ to XML to get data
XDocument doc = XDocument.Load(response.GetResponseStream());
XNamespace nsDataService = "http://schemas.microsoft.com/ado/2007/08/dataservices";
string title = doc.Descendants(nsDataService + "Title").First().Value;
REST call from Java script :
$(onPageLoad);
function onPageLoad() {
$("#GetSiteInfo").click(onGetSiteInfo);
}
function onGetSiteInfo() {
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/Web?$select=Title";
// execute AJAX request
jqxhr = $.getJSON(requestUri, null, OnDataReturned);
jqxhr.error(onError);
}
function OnDataReturned(data) {
var odataResults = data.d
var siteTitle = odataResults.Title;
$("#results").html("Title: " + siteTitle);
}
function onError(err) {
$("#results").text("ERROR: " + JSON.stringify(err));
}
function onPageLoad() {
$("#GetSiteInfo").click(onGetSiteInfo);
}
function onGetSiteInfo() {
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/Web?$select=Title";
// execute AJAX request
jqxhr = $.getJSON(requestUri, null, OnDataReturned);
jqxhr.error(onError);
}
function OnDataReturned(data) {
var odataResults = data.d
var siteTitle = odataResults.Title;
$("#results").html("Title: " + siteTitle);
}
function onError(err) {
$("#results").text("ERROR: " + JSON.stringify(err));
}
Creating List with Managed code and REST API :
// build request
Uri uri = new Uri(siteUrl + "/_api/web/lists");
HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
request.Accept = "application/atom+xml";
request.Headers["X-RequestDigest"] = FormDigest;
request.Method = "POST";
string body = Properties.Resources.CreateList_atom;
body = body.Replace("@Title", listTitle);
request.ContentLength = body.Length;
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(body);
writer.Flush();
// send request
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Uri uri = new Uri(siteUrl + "/_api/web/lists");
HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
request.Accept = "application/atom+xml";
request.Headers["X-RequestDigest"] = FormDigest;
request.Method = "POST";
string body = Properties.Resources.CreateList_atom;
body = body.Replace("@Title", listTitle);
request.ContentLength = body.Length;
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(body);
writer.Flush();
// send request
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
http://msdn.microsoft.com/en-us/library/office/fp142380(v=office.15).aspx
More Information :
Sharepoint Elements :
_spPageContextInfo this will be very useful while doing app model development.
spPageContextInfo, you’ll find it very helpful when writing SP JavaScript code
By using IE we can inspect _spPageContextInfo values.