Using the Excel Services REST API to Access Excel Data in SharePoint Server 2010
Summary: Learn how to access Microsoft Excel workbook data on Microsoft SharePoint Server 2010 using the Excel Services REST API.
Applies to: Excel Services | Office 2010 | SharePoint Foundation 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio 2008 | Visual Studio 2010
Provided by: Steve Hansen, Grid Logic (Visual Studio MVP)
The Excel Services REST API is a new feature of Excel Services that enables you to access Microsoft Excel workbook data by using a uniform resource locator (URL) address. Using the REST API, you can retrieve resources such as ranges, charts, tables, and PivotTables from workbooks stored on SharePoint Server 2010.
The REST API provides a flexible way to use Excel data. In addition to directly linking to Excel resources using the REST API, you can also make basic HTTP calls programmatically to use Excel data within external programs.
The code in this section shows how to use the Excel Services REST API using a simple console application example.
In the REST API, code is portable. There are no assemblies to distribute, no additional references are required in your project, and you do not have another object model to learn. As long as you understand the URL convention for accessing resources, you only have to build the URL and then call it with a standard HttpWebRequest object.
The following code example is from a simple console application. It demonstrates a basic pattern for retrieving resources from an Excel workbook on SharePoint Server 2010 using the REST API. The code example retrieves a range named "CustomerName" from a workbook named "Invoice.xlsx" that is stored in a document library named "Shared Documents".
Be aware that the GetRangeValue() function contains a semi-hard-coded string that is used to build the URL to the resource. To use this code, replace the <server> placeholder with the address of a SharePoint environment.
This section shows you how to correctly construct a REST URL to obtain information from a resource in Excel Services.
The key to using the REST API is in understanding how to construct the correct URL to a resource. A REST URL in Excel Services consists of three required parts and one optional part.
The first part is the location of the ExcelRest.aspx page. ExcelRest.aspx is the entry point to the Excel Services REST API. The second part is the workbook location relative to the ExcelRest.aspx page. The third part is the path of the requested resource inside the workbook. As an example, if the URL to the SharePoint Server is http://intranet.contoso.com and you wanted to retrieve a range named "CustomerName" from a workbook named "Invoice.xlsx" stored in Shared Documents, the URL for this would be as follows.
The optional part of the URL is a format parameter. The REST API can send range values in one of two ways. By default, the REST API returns range information as HTML code as shown in figure 1.
If you do not specify a format parameter, this is the format that is returned when you request a range resource. It is recommended that you explicitly request the format to avoid confusion. To specify the format, append ?$format=html to the end of the URL. For example, the CustomerName example earlier is as follows.
The REST API can also return range information in Atom Syndication Format. As shown in figure 2, this format is not directly accessed by Internet Explorer. However, if you right-click in Internet Explorer and View Source, you can view the data returned.
Although the Atom format is not immediately consumable in applications such as Internet Explorer, and because it is an XML representation of the data, the Atom format is better suited for programmatic consumption and manipulation. To request a range resource in Atom format, append ?$format=atom to the end of the URL. For example, to retrieve the CustomerName example in Atom format use the following code.
If you are unsure what resources are available in a given format, the REST API includes a discovery mechanism. To view this, you can use a special page called the Model page. For example, to view the model page for an Invoice workbook, the URL is as follows.
Then, if you click Ranges, the model displays the list of range names in the workbook.
You can click right through to the range to inspect its value. However, the model view specifies an Atom format as you come to the range level which cannot be displayed directly in Internet Explorer. When you locate the resource to embed or use elsewhere, copy the URL in the address bar of Internet Explorer and then paste it where you want it.
> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/b989266d-af0f-44a9-919d-febbfc6c6737]
About the Author
Steve has authored many white papers related to Office development and two books─Mastering Excel 2000 Premium Edition and Mastering Excel 2003 Programming with VBA─both published by Sybex.