Search code examples
sapui5ui5-tooling

How to get a version of UI5 framework within the controller?


I want to retrieve an UI5 version (e.g. 1.97.0-SNAPSHOT) inside of controller. The simplest way is to get a value of sap.ui.version, but it's a global variable, which I want to avoid.

I thought, sap.ui.VersionInfo would be a right choice, but looking at its API, I can't find any method, related to the retrieval of the UI5 version.

I also checked the controller's internals (not too deep) and sap.ui.Device, but no version number.

How can I retrieve the UI5 framework version without using a global variable?


Solution

  • If you're using OpenUI5, require sap/ui/core/Core and get an instance of sap/base/util/Version via Core.getConfiguration().getVersion().

    In case of SAPUI5, however, the patch version is not always same as the version of SAPUI5 core (sap.ui.core) since SAPUI5-exclusive libraries may contain patches in addition to OpenUI5 libraries. See this graphic SAPUI5 library patch versions (e.g. v1.96). To get the correct SAPUI5 version reliably, require the module sap/ui/VersionInfo. Its method load() will return a promise that resolves with an object - same as https://<host>/resources/sap-ui-version.json - containing all information about the current framework including its version.

    // VersionInfo required from "sap/ui/VersionInfo"
    // VersionUtil required from "sap/base/util/Version"
    
    // Within an async function:
    const { version } = await VersionInfo.load();
    const versionUtil = new VersionUtil(version);
    

    As you can see, it's also possible to combine the retrieved version string with the module sap/base/util/Version which provides methods such as getMajor(), getMinor(), getPatch(), compareTo(...), inRange(...), etc.

    See section "SAPUI5 Version vs. OpenUI5 Version (Core Version)" from the documentation topic Versioning and Maintenance of SAPUI5.


    ⚠️ Known issue

    When using the module sap/ui/VersionInfo in a standalone UI5 app (e.g. built with ui5 build self-contained -a), the top version value from the generated resources/sap-ui-version.json is the app version instead of the version of the framework. There is currently no reliable way for standalone SAPUI5 apps to determine the correct patch version.

    See the issue SAP/ui5-tooling#353.