I have a visualforce page on which I want to conditionally render two output panels, one is rendered if the current user is switched to lightning experience and the other is rendered if the user is switched to salesforce classic experience.

The page can be opened from a link contained in a chatter post and in an email sent to the user from apex code.

The solution that I have currently implemented is to check the value returned by "document.referrer" on page load. If the value contains "lightning.force.com" I display the first output panel (let's say Lightning Output Panel) and if not then I display the second output panel (let's say Classic Output Panel), but this doesn't work in case the user tries to open the page from the link in the email because here the document.referrer always returns a blank string and always the second output panel (Classic Output Panel) is displayed.

I have also tried

var isSF1 = typeof sforce !== 'undefined';

but in my case "sforce" is always null because I am opening the page from a url I built in my apex controller. If I try this on a page opened from a record detail page button/link (or from anywhere within the org for the matter) then it works just fine.


This technique I am using is this:

<apex:variable var="classic" value="{! $User.UIThemeDisplayed == 'Theme2' || $User.UIThemeDisplayed == 'Theme3' }"/>

<apex:outputPanel layout="none" rendered="{! classic }">

<apex:outputPanel layout="none" rendered="{! !classic }">

documented in $User.UITheme and $User.UIThemeDisplayed.

  • Thanks for the quick response Keith but unfortunately that didn't work, I am still seeing the classic output panel in both the cases. I created a new visualforce to test the solution you provided. The page only contains the code that you posted in your answer. – Gaurav Saini Jul 13 '17 at 7:46
  • @GauravSaini Weird. If you open the page directly using its URL i.e. /apex/YourPagerName does it respect the setting in that case? – Keith C Jul 13 '17 at 8:02
  • Yes, it sure does, but in my case I am opening the page from a link included in the mail body of an email sent through apex code. I think these are two entirely different things. – Gaurav Saini Jul 13 '17 at 8:04
  • @GauravSaini In the email case, is the link to an unauthenticated page? If you want the page to respect the choice of the user that sent the link you can include a parameter in the URL to choose the styling. Don't see why the choice of the currently logged in user who is reading the link wouldn't be respected (if the page is authenticated). – Keith C Jul 13 '17 at 8:17

You can directly $User.UIThemeDisplayed in VisualForce. If you are using Lightning Experience then you have to write apex method to detect the theme. You can click on below link for more:- https://developer.salesforce.com/blogs/isv/2016/04/introducing-ui-theme-detection-for-lightning-experience.html

  • Great link Manjot. +1 from me. – Keith C Jul 13 '17 at 7:38
  • The approach provided in the link didn't work. In my case I am opening the page from a link included in the mail body of an email sent through apex code. The solution you provided will work fine for any page I open from within my salesforce org (eg: detail page button/link), but not for pages opened from links in email sent to the user. – Gaurav Saini Jul 13 '17 at 8:07

Perhaps what you're looking for is the User.UserPreferencesLightningExperiencePreferred field? You could get that in your Apex controller and then use it in your VF.


It doesn't really say whether or not the user in currently in LEX or Classic but, for your email link scenario, it might be just what you need.

  • Hi @Aidan , I tried using UserPreferencesLightningExperiencePreferred already. I got stuck in a scenario where if Lightning Experience is disabled (here by disabled I mean disable lightning experience from Setup in Salesforce Classic > Get Started in the Lightning Experience Migration Assistant tile at the top of the menu > Turn It On tab > Disable) and the field is set to true then the output panel which should be shown in lightning experience is displayed when I open the page in classic experience. – Gaurav Saini Jul 13 '17 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.