Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
Showing results for 
Search instead for 
Did you mean: 
Active Contributor
Hello guys

We have a few SAPUI5 apps and they share common code, so I will show you how to create a library and how to access it in your other apps.

Basically the Library is just a SAPUI5 app or normal BSP. We create all our files that we want to reuse and deploy them to the APAB server/ Gateway (as we would any other SAPUI5 app).

So let's start.


We create the project, and inside it we create a JS file that looks like this:

], function (Object) {
"use strict";
return Object.extend("libraryApp.utilities.LoggedOnUser", {
getLoggedOnUser : function () {
console.log("Inside LoggedOnUser.getLoggedOnUser");
// your code here
return "Antonette Venter" ;

Now we want to reuse this function in all of our other apps:


First we have to register the path to our library app to be able to access the files inside it:


<!-- Init -->		
// Register LibraryApp (to be able to access the functions in Component sap.ui.define)"libraryApp.utilities", "../zlibraryApp/utilities");

// Load Component
sap.ui.getCore().attachInit(function () {
], function (Shell, ComponentContainer) {
new Shell({
app: new ComponentContainer({
name: "consumerApp",

Once registered, we will be able to access the files via Component:


"libraryApp/utilities/LoggedOnUser" // Here we define the JS files
], function (UIComponent, JSONModel, LoggedOnUserJS) { // . . . and here
"use strict";

return UIComponent.extend("consumerApp.Component", {
id: "consumerAppId",
metadata : {
manifest: "json"
* The component is initialized by UI5 automatically during the startup of the app (sap.ui.getCore().attachInit()) and calls the init method once.
* @public
* @override

init : function () {

//<!-------- Component and Router apply init ----------->

console.log("Component Initialize");

// Call the init function of the parent
console.log("Component - before prototype.init.apply(this, arguments)");
UIComponent.prototype.init.apply(this, arguments);
console.log("Component - after prototype.init.apply(this, arguments)");

// Initialize Router
console.log("Component - Router Initialize");

//<!-------- Using the JS functions from libraryApp ----------->

this.getUser = new LoggedOnUserJS(); // Create new instance. Like new sap.m.Button
// And now we can call the functons inside the JS file as we please 🙂


And there you go! 🙂

Please don't be shy to comment if you have also found other ways to do the same thing.

To see how to reuse controls, check out this blog: SAPUI5 How To: Reuse parts of a SAPUI5 application in other/multiple SAPUI5 applications


Labels in this area