Enable/Disable Accordion based on current user

I would like to Enable/Disable a Staff Only accordion based on current user, but only if the current user has an email address that ends in @contosso.com All other users cannot use or see the Staff Only accordion.

Fellow Community Member "smithme" has a post that suggests I may want to check against this:

web.currentUser

And I have successfully used the show/hide function, and also the enable/disable function, but I am not sure how I would combine the two.

//This shows or hides Staff Only Accordion

fd.spRendered(function() {

function enableOrDisableAccordionTabs() {
    if (fd.field('web.currentUser').value == '*.@contosso.com') {
        // Enable Staff Only Accordion
        fd.container('Accordion2').$children[0].disabled = false;
    } else {
        // Disable Staff Only Accordion
       fd.container('Accordion2').$children[0].disabled = true;
    }
}

// Calling enableOrDisableAccordionTabs when the user changes CurrentUser
fd.field('web.currentUser').$on('change',enableOrDisableAccordionTabs);

// Calling enableOrDisableAccordionTabs on form loading
enableOrDisableAccordionTabs();

});

I use this CSS with the JS to hide all Disabled Accordions

.accordion .disabled{
display: none; /* disabled children don't show up */
}

Hello @shedev,

The example in the post is for the form routing.

If you want to show/hide the accordion tab based on the current user email, please try out the following code. Please make sure that you are using the internal name of the control in the code.

fd.spRendered(function() {

    function enableOrDisableTab() {

            var userEmail = _spPageContextInfo.userEmail;
        if (userEmail.includes('@contosso.com') == true) {
            // Show the first tab
            fd.container('Accordion2').$children[0].disabled = false;
        } else {
            // Hide the first tab
            fd.container('Accordion2').$children[0].disabled = true;
        }
    }

    // Calling enableOrDisableTab on form loading
    enableOrDisableTab();

});
1 Like