Enterprise Resource Planning Blogs by SAP
Get insights and updates about cloud ERP and RISE with SAP, SAP S/4HANA and SAP S/4HANA Cloud, and more enterprise management capabilities with SAP blog posts.
Showing results for 
Search instead for 
Did you mean: 
Product and Topic Expert
Product and Topic Expert

Using C# to call Odata api in S4 HANA CLOUD Method 1

In some situation like integrating with S4 hana cloud, we need to use C# to call odata in S4 hana cloud(s4hc). For example we need to integration a third party Mes with S4HC and the Mes system is developed in C# . Customer decided to use point to point integration without integration middle ware .Then we need to use C# to call odata in S4HC .

The assumption is that the reader has done the following part :

1, Have created communication system , communication user and communication arrangement in S4HC .

2, Have tested the odata api from postman for the odata api .

The following is the step to call odata api in s4hc by coding in c# with visual studio 2012 .

1 Create a new project in VS2012

[File] > [New]>[Project]

Or Ctrl+ Shift+N

The following is the C# code :

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Net;

using System.IO;

using System.Xml;

namespace ConsoleApplication2


class Program


static void Main(string[] args)




string url = "https://my3xxxxx-api.saps4hanacloud.cn/sap/opu/odata/sap/API_BILLING_DOCUMENT_SRV/A_BillingDocument";

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);

httpRequest.Method = "GET";

string authorization = "UserName" + ":" + "PassWord";

string base64 = Convert.ToBase64String(Encoding.Default.GetBytes(authorization));

httpRequest.Headers.Add("Authorization", "Basic " + base64);

using (HttpWebResponse myres = (HttpWebResponse)httpRequest.GetResponse())


StreamReader sr = new StreamReader(myres.GetResponseStream(), Encoding.UTF8);

string resstring = sr.ReadToEnd();

XmlDocument doc = new XmlDocument();


XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);

nsmgr.AddNamespace("a", "http://www.w3.org/2005/Atom");

nsmgr.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");

nsmgr.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");

XmlNodeList nl = doc.SelectNodes("//m:properties", nsmgr);

foreach (XmlNode n in nl)


Console.WriteLine(n.SelectSingleNode("//d:BillingDocument", nsmgr).InnerText);

Console.WriteLine(n.SelectSingleNode("//d:CreationDate", nsmgr).InnerText);

Console.WriteLine(n.SelectSingleNode("//d:CreationTime", nsmgr).InnerText);





Console.WriteLine("press any key to continue....");



catch (Exception e)











After change the correct URL , user name and password, execute the program .

Explanations to the coding :

nsmgr.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");

It is name space in the message respond .

Best regards!

Jacky Liu