8

Down the rabbit hole: How to get current sap-client programmatically from a cont...

 1 year ago
source link: https://blogs.sap.com/2023/02/07/down-the-rabbit-hole-how-to-get-current-sap-client-programmatically-from-a-controller-within-sapui5./
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
February 7, 2023 2 minute read

Down the rabbit hole: How to get current sap-client programmatically from a controller within SAPUI5.

Hi everyone!

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.

Screenshot-2023-02-01-100810.png

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?

Screenshot-2023-02-01-102601.png

Same idea I had: build a custom service. It seems ChatGPT likes CAPM.

So went ahead and open the fiori launchpad, navigated to a standard SAP app and opened the google inspector on the network tab. Refreshed the view and got a list of all the requests:

Screenshot-2023-02-01-101148.png

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.

Screenshot-2023-02-01-101914.png

With this in mind… it was quite easy to retrieve the sap-client.

Screenshot-2023-02-01-102807.png

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.

Follow the SAP Fiori environment Topic page on the SAP Community, post and answer questions on SAP Answers, and read other posts on the topic on the SAP Blogs.

Cheers,

Facundo


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK