Skip to Content
Technical Articles
Author's profile photo Krishna Kishor Kammaje

Fiori Variant Management: Set a Variant as default to ALL users

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.!

Assigned Tags

      13 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Jason Scott
      Jason Scott

      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.

      Author's profile photo Georgi Nushev
      Georgi Nushev

      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

      Author's profile photo Jan Mattfeld
      Jan Mattfeld

      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);
      				});
      Author's profile photo Alexander Kostarev
      Alexander Kostarev

      Great advice, thank you so much 🙂

      Author's profile photo Mio Yasutake
      Mio Yasutake

      Thanks for sharing this blog!

      Author's profile photo Rakesh Singh
      Rakesh Singh

      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

      Author's profile photo Michael Smith
      Michael Smith

      Is there a way to create the new variant programmatically before setting it as default?

      Author's profile photo Stefan Heitzer
      Stefan Heitzer

      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

      Author's profile photo rajesh maripeddi
      rajesh maripeddi

      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

      Author's profile photo Dan Connor
      Dan Connor

      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

      Author's profile photo Girish Kumar Gupta
      Girish Kumar Gupta

      Agree, didn't work for me either.

      Author's profile photo Cengiz Türkoglu
      Cengiz Türkoglu

      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

      Author's profile photo Quentin COUVREUR
      Quentin COUVREUR

      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