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!
cancel
Showing results for 
Search instead for 
Did you mean: 
kammaje_cis
SAP Mentor
SAP Mentor
40,417
Consider that you have created a Fiori application (Smart or not) with multiple filters and tables. In such a case you might want to help the users by providing default values to various filters, table columns etc. You can achieve that by creating Global Variants. But users still have to explicitly select this Global Variant you created and set as 'default'.  There is no SAP provided way as of now to set a Global Variant as default to all the users. Hope SAP provides this feature one day. Till then here is the programmatic solution.

Important note: Like many of my blogs, this is a hack. Which means this is not an SAP documented supported scenario. So SAP does not guarantee that these APIs will work seamlessly across upgrades. Thanks prasita.prabhakaran for pointing that.

So lets get started!

Step 1. Create a new Workbench Transport (TCode SE10) in your Gateway system. This is used to store the Global Variant that you are going to create and move it all the way to Production.

Step 2. Open the Fiori application under consideration. Set the application status by selecting various filter values, selecting columns of tables, selecting width of layouts so on.

Step 3. Save this Variant as a Public Variant, by selecting the checkbox as shown below. Moment you check this box, you will be prompted for a transport request. Select the transport request you created in Step 1.



Step 4. At this point, you have the Global Variant and it is available to all users. Now you need to set this as default to all. First we need to find the technical id of this Variant. Refresh the application with Developer Tools open. Under network tab, filter the requests by search term 'lrep/flex'. Under the Preview tab, open the response. Under 'changes', there will be two entries with 'fileType' as "variant". First entry is for the SAP delivered "Standard" variant. Second one is for the new Global Filter you created in Step 3. Copy the content under property "fileName". This is the technical name of your Global Variant.



 

Step 5. Open the Fiori application where you need to set the default Variant. If it is a free-style application, you can do it in onAFterRendering event of the Controller. If it is an OVP, you need to create a custom controller and write in onAfterRendering event handler.

 
onBeforeRendering: function(){
//Get reference to a control which has Variant management
var oSmartFilter = this.getView().byId("ovpGlobalFilter");

//Set the Global Variant as the current Variant
oSmartFilter.getVariantManagement().setCurrentVariantId("id_1535046664297_171_page");
}

 

Step 6. If the user has already defaulted a Variant, then you do not want to overwrite that Variant. So ensure that you set the default Variant only if the user does not have a default Variant. Check the self explanatory code below.
onBeforeRendering: function(){
//Get reference to a control which has Variant management
var oSmartFilter = this.getView().byId("ovpGlobalFilter");

//Ensure that there is no default Variant set by the user.
//In such a case, do not set default Variant.
var sDefaultVariantKey = oGlobalFilter.getVariantManagement().getDefaultVariantKey();

//If No variant is set, default variant is "standard"
if (sDefaultVariantKey !== "*standard*"){
return;
}

//Set the Global Variant as the current Variant
oSmartFilter.getVariantManagement().setCurrentVariantId("id_1535046664297_171_page");
}

 

Hope it was helpful !!

 

Approach 2: (Update on Feb 19th, 2019)

If you are explicitly using control SmartVariantManagement in your application, you can do it in a better way by enhancing the SmartVariantManagement control.

Step 1. Create a 'controls' folder inside your 'webapp' folder and create a file by name 'SmartVariantManagement.js'.

Step 2. Enhance the SmartVariantManagement control as below.

Copy paste the below code into SmartVariantManagement.js. Ensure that <your component name> is replaced by your actual component name.
sap.ui.define(
['sap/ui/comp/smartvariants/SmartVariantManagement'],
function (SmartVariantManagement) {

return SmartVariantManagement.extend("<your component name>.controls.SmartVariantManagement", {
metadata: {
properties: {
fallbackDefaultVariant: {
type: "string"
}
}
},
renderer: function (oRm, oControl) {
SmartVariantManagement.getMetadata().getRenderer().render(oRm, oControl);
},
_getDefaultVariantKey:function () {
var defaultVariant = "";
if (this._oControlPersistence) {
defaultVariant = this._oControlPersistence.getDefaultVariantIdSync();
if (defaultVariant === "*standard*" || defaultVariant === "") { //No default variant was set by the user
defaultVariant = this.getFallbackDefaultVariant();
}
}
return defaultVariant;
}
});
}
);

Step 3. Add the namespace for your custom control in your XML view.
xmlns:cp="<your component name>.controls"

Step 4. Replace SmartVariantManagement with your new control.
<!--<smartVariantManagement:SmartVariantManagement id="pageVariantId" persistencyKey="PageVariantPKey"/>-->
<cp:SmartVariantManagement id="pageVariantId" persistencyKey="PageVariantPKey"/>

Step 5. Pass your default variant id in the above declaration as below.



 

Thats it.!
14 Comments
js2
Product and Topic Expert
Product and Topic Expert
Great stuff Krishna. What a shame SAP are unable to provide such basic functionality for their standard fiori apps...

If you save the variant as a tile it just adds a parameter variantKey to the URL. This can be configured into a tile in the designer allowing you to default this variant for ALL users. However - its a "hard" default - the user cannot manually change the default to something else as the url parameter overrules it.
Georgi
Explorer
0 Kudos
Hi Krishna,

thanks for the blog post.

After creating a public Variant, every user sees it in the "Manage Variants" option and can delete it.

Is there a way to limit the users to delete a public Variant, so that only the owner can do it?

 

Greetings,

Georgi
jmattfeld
Participant

Variant changes should be restricted to own variants, as described in the Fiori Guidelines.

Yet, it seems the standard only restricts changes for non-key users, see SAP Notes 2655097, 2658662 and 2666625.

You can restrict variant changes manually:

oVariantManagement.getVariantItems().filter(function (oVariant) {
return oVariant.getAuthor() !== sap.ushell.Container.getService("UserInfo").getId();
}).forEach(function (oForeignVariant) {
oForeignVariant.setProperty("readOnly", true);
});
MioYasutake
Active Contributor
0 Kudos
Thanks for sharing this blog!
0 Kudos

Hi Krishna,

Is this method also applicable for extending standard apps “procurement overview page” to set filter variant as default for everyone.

I am asking because i tried using the same logic by using adaptation project method by extending controller, but it’s not working.

Now i doubt the above logic is applicable on standard app, but limited to custom ovp apps only.

Please confirm.

Thanks,

Rakesh

Great advice, thank you so much 🙂
michael_smithe5
Participant
Is there a way to create the new variant programmatically before setting it as default?
stefan_heitzer
Participant
Hi everybody,

 

is there meanwhile a way how do achieve this without programming something?

I'm talking about a standard way through maybe the launchpad or the backend itself.

 

Greetings
0 Kudos
Hello Krishna,

Its an informative article. But when I tried this in my system, it is not asking me for Transport request. This will be used for transporting to other system. Is there anything missing in my role.

Regards,

Rajesh
Dan_Connor
Explorer
Hello!
I've tried with smartVariants in SmartTable and SmartFilterBar and isn't working in 2022 (UI5 Version 1.71.11).

Didn't try with the variant at the 'page level' like the example, but I guess it's possible that we can't do this approach anymore.

It seems that SAP does not provide a solution for this case and forces each user to select his SmartVariant and check it by default.

Appreciated for your presentation.

Regards,
Dan
Girish_Kumar_Gu
Product and Topic Expert
Product and Topic Expert
0 Kudos
Agree, didn't work for me either.
ct-ch
Explorer
0 Kudos

Hi Krishna / Hi All.

Thanks for that great blog. A question: I'm able to set the variant as "default" dynamically. But how can I fire now the "switch" to this variant so it's shown instead the standard? I've tried it with "fireManage(oVariantInfo);", but it works only after the second or third time, and that only when I navigate back to the fiori launchpad and again to the app.

Here my example:

let oVariantManagement = this.getView().byId("NotificationListVariantMng");
let oNotificationTable = this.getView().byId("NotificationTable");
let oVariantJSON = null;
let sVariantKey;
let sVariantId = "id_1657605931121_613_page";
let bFound = false;

if(oVariantManagement) {
try {
oVariantManagement.getVariantItems().forEach(function(item, index){
if (item.mProperties.key === sVariantId){
sVariantKey = item.mProperties.key;
oVariantJSON = oVariantManagement.getVariantContent(oVariantManagement, sVariantKey);
bFound = true;

throw "break";
}
});
} catch(e) {
if(e === "break") {
throw e;
}
}

if(bFound) {
oVariantManagement.setDefaultVariantKey(sVariantKey);

if(oNotificationTable) {
oNotificationTable.applyVariant(oVariantJSON); // do I have to apply here?
}

oVariantManagement.fireManage(oVariantJSON); // is that the right way to fire a switch?
}
}

Thanks to all.

Regards,

Cengiz

former_member833352
Discoverer
0 Kudos
Hello, thanks for this great article.

Question : the "standard" variant is set as favorite and cannot be removed. Is it possible to do the same with 1 custom variant ? (I mean to prevent end users from removing "favorite" on a custom variant created by the central team) ?

Regards, Quentin
0 Kudos
Hello,

I cannot recomment the above flow since it tackles with interas of openui5 and also overrules the tools ment to set the defaults.

Each customer can set the default for all users via the key user tools at runtime in a system and the same hold true for developers by using the Fiori Tools.

In addition, end users may also set their own default if it better fits their use case. With the programatically setting of the default, a user will still be able to change the default. Next time the app starts, a racecondition is created and if the users default is set before the above mentioned code is executed, it is overwritten.

Reards,
Christian
Labels in this area