Open Data Protocol (OData) (see also [MS-ODATA]: Open Data Protocol (OData) is a data access protocol for web applications. A JavaScript library such as datajs is used to access an OData source.
The below sample uses an OData source from OData.org. There is a list of sample services available at http://www.odata.org/ecosystem.
Note the Kapsel SDK as of SP 11, supports the V2 version of OData.
One of the nice features of OData is how simple it is to access an OData source. Simply enter the OData URL with a $metadata parameter to see a description of the service.
http://services.odata.org/V2/OData/OData.svc/$metadata.
It is also possible to use a free online tool such as xodata to visualize the metadata.
This describes that there are three entities named Products, Categories, Suppliers, what fields are in each entity and the associations between them.
To access the data, specify which data to read such as http://services.odata.org/V2/OData/OData.svc/Categories. As seen below, there are three categories, Food, Beverages and Electronics.
The data is formatted as either atom(xml) or json. The json format is less verbose. http://services.odata.org/V2/OData/OData.svc/Categories?$format=json
The URLs to navigate the association between categories and products are shown in this link http://services.odata.org/V2/OData/OData.svc/Categories(2)/Products?$format=json which shows the products for category 2 or Electronics.
Here are a few more examples.
First three products http://services.odata.org/V2/OData/OData.svc/Products?$top=3&$format=json
Fourth product http://services.odata.org/V2/OData/OData.svc/Products(4)?$format=json
Products that start with the letter F http://services.odata.org/V2/OData/OData.svc/Products?$filter=startswith(Name, 'F') eq true&$format=json
The following links may also be helpful.
URI Conventions
Basic Tutorial
The below steps demonstrate how to create a simple OData web app.
Create a folder named C:\ODataSample.
Create a file named ODataSample\products.html and paste in the following code.
<html>
<head>
<script src="datajs-1.1.2.min.js"></script>
<script>
function successCallback(data, response) {
var productsTable = document.getElementById("productsTable");
for (var i = 0; i < data.results.length; i++) {
var row = productsTable.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
cell1.innerHTML = data.results[i].Name;
cell2.innerHTML = data.results[i].Description;
cell3.innerHTML = data.results[i].Price;
}
}
function errorCallback(e) {
alert("An error occurred" + JSON.stringify(e));
}
function init() {
OData.defaultHttpClient.enableJsonpCallback=true; //http://datajs.codeplex.com/wikipage?title=OData%20Security&referringTitle=Cross%20Domain%20Requests
OData.read("http://services.odata.org/V2/OData/OData.svc/Products", successCallback, errorCallback);
}
</script>
</head>
<body onload="init()">
<table id="productsTable"><tr><th>Name</th><th>Description</th><th>Price</th></tr></table>
</body>
</html>
Download the latest datajs file (datajs-1.1.2.min.js) from datajs and place it in the folder ODataSample.
The datajs JavaScript library provides a read method that takes an OData URL and returns the data as a JavaScript object.
See also the example Cross-Site Request Forgery (CSRF)in the security section that performs create, read, update and delete operations.
SQL Anywhere 17 is included with SMP 3.0 SP10 Server and can be used to expose a database table as an OData service. The following instructions demonstrate how this can be accomplished.
Start
C:\SAP\MobilePlatform3\Server\db\sa\smp3\win32\Bin64\scjview.exe
Create a new database. Tools > SQL Anywhere 17 > Create Database Choose to save the database to the following file.
C:\SAP\MobilePlatform3\Server\db\sa\smp3\odata.db
Specify the DBA user and password. I used my first name dan.
jConnect is not necessary.
Uncheck Connect to the new database.
After the database has been created, start it on a SQL Anywhere network server enabling it to be used as an OData service.
cd C:\SAP\MobilePlatform3\Server\db\sa\smp3\win32\Bin64
dbsrv17.exe -xs odata(ServerPort=8085) ..\..\odata.db
Start the dbisql tool.
C:\SAP\MobilePlatform3\Server\db\sa\smp3\win32\Bin64\dbisql.exe
Connect to the running database server. Select Connect to a running database on this computer and fill in the user name (dan), password and Server name (odata).
Execute the following command to create a new table.
DROP TABLE IF EXISTS dan.CarrierCollection
CREATE TABLE dan.CarrierCollection(carrid char(3) NOT NULL PRIMARY KEY, CARRNAME char(20))
INSERT INTO dan.CarrierCollection(carrid, CARRNAME) VALUES('AA','American Airlines')
INSERT INTO dan.CarrierCollection(carrid, CARRNAME) VALUES('AB','Air Berlin')
INSERT INTO dan.CarrierCollection(carrid, CARRNAME) VALUES('AC','Air Canada')
INSERT INTO dan.CarrierCollection(carrid, CARRNAME) VALUES('AF','Air France')
Create a service model file in the following location with the below contents.
C:\SAP\MobilePlatform3\Server\db\sa\smp3\service.osdl
service {
entity "dan"."CarrierCollection";
}
Execute the following command in dbisql to create an OData service based off of the CarrierCollection table.
DROP ODATA PRODUCER IF EXISTS ODataProducer
CREATE ODATA PRODUCER ODataProducer
AUTHENTICATION Database
MODEL FILE '../../service.osdl'
SERVICE ROOT '/'
Open the following URLs to view the OData service.
http://localhost:8085/
http://localhost:8085/$metadata
http://localhost:8085/CarrierCollection?$format=json
Note the following command can be used to change a password.
ALTER USER dan IDENTIFIED BY pwd2;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
24 | |
11 | |
10 | |
8 | |
8 | |
7 | |
6 | |
6 | |
5 | |
5 |