Tuesday, September 04, 2012

Exploring the REST API options in SharePoint 2013– Part I

In SharePoint 2010 the Client Side Object Model (CSOM) was made available through the _vti_bin/client.svc. Client.svc wasn’t really build to be accessed directly but calls would go through supported entry points (proxies)  being the 3 flavours of the CSOM:

  • Silverlight Client Side Object Model
  • Javascript Client Side Object Model
  • .Net Client Side Object Model

Unfortunately the Javascript CSOM was harder to use then the managed version and also more limited in functionality. SharePoint 2010 also provided oneREST/oData enabled service called  ListData.svc also referred to as WCF Data Services (See Introduction to oData and SharePoint Server 2010 for more info)

In SharePoint 2013 the client.svc service is extended with REST capabilities and it now support direct access from REST clients which makes it easier for javascript and non .NET code to access it.  There is also a new shorthand notation for accessing the REST API where CSOM URLs can go through the _api folder.

This means that you can now replace

http://sharepoint/_vti_bin/client.svc/web

with

http://sharepoint/_api/web

The new SharePoint 2013 REST API follows the oData protocol specification but also extends it in some ways to support more complex SharePoint specific operations. The SharePoint 2013 Preview REST service responses are formatted by using the Atom protocol by default. But it is also possible to use HTTP Accept headers that enable you to specify that the response is returned in JSON format which is easier to use in Javascript code.

Since a number of REST calls are simple GET operations it is possible to learn the syntax by creating the URL in a browser and looking at the ATOM response being returned. Next are a number of examples to get you started:

  • https://[yoursharepointsite]/_api/web/ – access a specific site and retrieves different properties
  • https://[yoursharepointsite]/_api/web/?$select=Title,Id – only retrieves the Title and ID for a specific site
  • https://[yoursharepointsite]/_api/lists  - retrieves all lists on a specific site
  • https://[yoursharepointsite]/_api/lists/getByTitle(‘Consultants’) – retrieves a single list based on the title used
  • https://[yoursharepointsite]/_api/lists/getByTitle(‘Consultants’)/Items – retrieves all items in the list with all the fields returned
  • https://[yoursharepointsite]/_api/lists/getByTitle(‘Consultants’)/Items/?$select=Title,LastName – retrieves for all items in the list only the Title and LastName field
  • https://[yoursharepointsite]/_api/web/lists/getByTitle('Consultants')/Items/?$select=Title,FirstName&$filter=startswith(Title,'C') – retrieves  items in the list where the Title starts with a C and retrieves the Title and LastName field

In a next post I’ll explore the extensions in the REST API for SharePoint Server 2013.

More information:

No comments: