Getting oData using Powershell

Here and there you will get in contact with oData service providers.

The Open Data Protocol (OData) is a data access protocol built on core protocols like HTTP and commonly accepted methodologies like REST for the web.

There are various kinds of libraries and tools can be used to consume OData services

This protocol allows you do make CRUD operations on the web data sources.

For the time being this will illustrated only the GET (READ) data features.

For example SharePoint is natively an oData provider.

But you can access a few Public oData providers. Here I will give you a few examples of oData.org public data sources.

image

If you go to this link above, you can see that it exposes a few services endpoints.

To consume these we will use PowerShell, of course Smile

Here we go :

CLS

$obj1 = Invoke-RestMethod -uri "http://services.odata.org/OData/OData.svc/Products?`$format=json"
$obj12.value | Select Name, Description, Price

This is the result :

image

Here is an other one to grab from oData

CLS

$url = "http://services.odata.org/v4/TripPinServiceRW/Airports"

$oOdata = Invoke-RestMethod -Uri $url 

$oOdata.value

This is the result :

image

As you can see it returns information of the Airports and apart from the “location” we see as well the “address” information.

Opposed to the first example above we will the COM like notation to access all data elements.

To retrieve the location data replace the .value by the below code.


# $oOdata.value

$oOdata.value.location

This is the result :

image

To retrieve the CountryRegion data replace the .location.city or just the Aiport Name by the below code.


$oOdata.value.location.city

write-host ""

$oOdata.value.name

This is the result :

image

This brings us to the fabulous LINQPad Tool that is able to consume oData sources

image

LINQPad can do a lot more that consuming oData sources. It was build to consume data sources using LINQ (Language-Integrated Query).

Unfortunately PowerShell can’t use LINQ natively.

But you can use the PowerQuery add-in.  Winking smile

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: