Consuming an SAP Odata Service with C#

Consuming an SAP Odata service with C# is is fairly simple to do. There are several different ways to do that. In this example, I will use base C# classes to retrieve the data and then use a C# library named JSON.Net to parse the data. I’ve written a short example here of how to return back the data.  For my SAP system, I am using an SAP instance I have installed locally which supports the NetWeaver Gateway.

The instance of SAP ships with some sample applications including the old favorite Flight application.  The NetWeaver Gateway will supply us back application data through our web browser. We can browse the types of data that can be returned for this example using this url substituting in your server name and appropriate port below:

http://<server name here>:8042/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/

That will yield the following result:

As you can see there are several object collections available related to the sample flight data. Here is the url we will be working with for this example:


On my system this returns the following information in JSON format and with the parameter 5 in the url above, this returns a list of 5 carriers.

Next we have to take these results and put them into a format that we can use in C#.  We could use the Microsoft JSON related libraries to parse the data but I chose to use JSON.Net which is a popular .NET library for working with JSON data. You can find  here

JSON.Net lets you deserialize JSON data. When you deserialize it using JSON.Net, the library maps the field values it finds by name into object classes that you create in C#. For this example above we have an airline carrier so I needed to create an airline carrier class.

Here is the class that will map out the carrier details. If you look at the details of this class, it matches the format of the JSON data that is displayed above.  The key class here is the CarrierCollectionD class. You can see it contains a List of CarrierCollectionResults. That is our collection of return values that we would need to iterate through.

Now, onto proceeding to getting this data. For that we will use a basic WebClient from C#. There are several ways you can request the data. WebClient is fairly simple because it provides the DownloadString method which downloads all of the JSON data into a single string you can store in a variable.

I then take that data and then create a simple C# DataTable instance to store the data. I just did this so I could show some code on how to map out the results to the class I created earlier.

When I run this program then, I get the following result when I inspect the DataTable that has been created.


Going forward I am going to explore other methods of retrieving this data. SAP offers UI5 to also allow you to retrieve the OData data but sometimes you like to use other tools to get the data, hence this article. Happy coding!

I would like to give this author credit – because without his post, I would not have been able to do this work that I have here.