Down the rabbit hole: How to get current sap-client programmatically from a controller within SAPUI5.
As a sapui5/abap-rap developer I found my self a couple of times on the situation where I needed to know the sap client of the current system. I’ve taken different approaches through the years, but recently a found what seems to be a better path. I hope that sharing my experience and solution can help other developers who are facing similar challenges. Also do please comment if you feel this new approach is better or if it feels kind of “hacky” to you.
So I was building an utility tool to automate recurring tasks and needed to call an standard sap odata service that required the sap-client as an URL parameter.
My first idea was to build a new entity set on the oData service with a single record to keep environment variables and then expose sy-mandt from there. I’ve taken this approach before and I know it works quite well and is clean.
From a technical point of view it was not really a challenge. However I wasn’t 100% comfortable with it. I mean standard apps, do figure out the sap-client some how, don’t they? Why re-invent the wheel with an additional OData entity.
I did a little research online but coudn’t find a good article on how to get it. I explored using sap.ushell.Container. It had helped me before to get the current User ID, but no luck there either.
I even asked ChatGPT about it, cause, why not?
Same idea I had: build a custom service. It seems ChatGPT likes CAPM.
To my surprise on the 15th call there was a call to “manifest.appdescr” with the urlParameter “sap-client=100”. So it had to happen before that.
start_up sounded like a good candidate. But no, no sap-client info was provided from the backend on body or headers.
And here is the deal: none of the other calls are odata service calls.
With a bit of luck I ended up going to ui.html. The second call on the list.
And then it showed up. A meta tag on the header.
With this in mind… it was quite easy to retrieve the sap-client.
I hope someone finds this handy.
So I came up with a new approach for obtaining the SAP client by inspecting the UI HTML of SAP app in the Fiori Launchpad rather than building a custom entity on an oData service. What do you think? Is this solution “hacky”? Should I take the building the custom entity path? Please share your feedback and thoughts on the comments section. Please share your feedback or thoughts in the comments section.
To stay updated on similar content and topics, follow my profile.