Product Information
Using C# to call Odata api in S4 HANA CLOUD Method 2
Since currently S4HC odata version is 2.0, I think we can not use odata client generater (it works for Odata v4).
In my previous blog , I blog dow the method of using httprequest to call odata API in S4HANA Cloud and then use XML to extract the data . In this blog , I want to using a more efficient way( WCF ) to call odata api in S4HC . Although some associates have write a blog about this topic. But I want to give a simplist example .https://blogs.sap.com?p=194
By using WCF Visual studio generate a service class base the metadata file. The following is the steps :
1, Get metadata from postman ,and save the response to xml file .
2 Create a new project in VS :
Create a new project in VS2012
[File] > [New]>[Project]
Or Ctrl+ Shift+N
3 . Add service reference in project :
4. using code to use the service .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Security;
using System.Data.Services.Client;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
BillingService.API_BILLING_DOCUMENT_SRV_Entities context =
new BillingService.API_BILLING_DOCUMENT_SRV_Entities(new Uri(“https://my3xxxxx-api.saps4hanacloud.cn/sap/opu/odata/sap/API_BILLING_DOCUMENT_SRV”));
context.SendingRequest += new EventHandler<System.Data.Services.Client.SendingRequestEventArgs>(OnSendingRequest);
var billings = context.A_BillingDocument.Execute();
foreach (var billing in billings)
{
Console.WriteLine(billing.AccountingDocument + “|” + billing.BillingDocumentDate + “|” + billing.CompanyCode);
//Console.ReadKey();
}
Console.WriteLine(“loop finished”);
Console.ReadKey();
}
private static void OnSendingRequest(object sender, SendingRequestEventArgs e)
{
string authorization = “UserName” + “:” + “PassWord”;
string base64 = Convert.ToBase64String(Encoding.Default.GetBytes(authorization));
base64 = “Basic ” + base64;
// Add an Authorization header that contains an OAuth WRAP access token to the request.
e.RequestHeaders.Add(“Authorization”, base64);
}
}
}
5, Execute the program .
Best regards!
Jacky Liu
Good info, thank you for sharing.
thanks for your contribution
have you been able to perform also a Patch or Post ?
It tryed many time but with not success.. Could you help?