Skip to Content
Product Information
Author's profile photo Jacky Liu

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

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)

{

try

{

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();

doc.LoadXml(resstring);

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(“n”);

}

Console.ReadKey();

}

Console.WriteLine(“press any key to continue….”);

Console.ReadKey();

}

catch (Exception e)

{

Console.WriteLine(e.StackTrace);

}

finally

{

}

Console.ReadKey();

}

}

}

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

Assigned tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Priyank Kumar Jain
      Priyank Kumar Jain

      Thanks Jacky,

      the following links could be helpful for information on working with communication arrangements, in case someone reading is not aware

      https://help.sap.com/viewer/abfba1342cfb4832ab722fa041f6c4b7/1702/en-US/de63ea8996514f518d251f5ae188fb11.html

      https://blogs.sap.com/2017/11/09/setting-up-communication-management-in-sap-s4hana-cloud/

      Author's profile photo Jacky Liu
      Jacky Liu
      Blog Post Author

      Hi, Priyank,

      Thanks for your great suggestion !