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.
cancel
Showing results for 
Search instead for 
Did you mean: 
Jacky_Liu
Product and Topic Expert
Product and Topic Expert

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







3 Comments