Show/Hide Accordion Based on MS365 Group Membership

The following does not work for MS 365 Groups. We use this block elsewhere for a list of user email addresses but the same code doesn't recognize these MS 365 Group members. How many I extend MS 365 Groups to show/hide an accordion? Thank you.

fd.spRendered(function() {


    var currentUserLoginName=_spPageContextInfo.userLoginName;
    **var arr = ['MembersofMS365Group@domain.com'];**

//This works ==>   var arr = ['user1@domain.com', 'user2@domain.com', 'user3@domain.com'];
//This does NOT ==>   var arr = ['MembersofMS365Group@domain.com'];

    function enableOrDisableAccordionTabs() {

        if (arr.indexOf(currentUserLoginName) >= 0) {

                // 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 on form loading
    enableOrDisableAccordionTabs();
});

Dear @shedev,
You need to check for the SharePoint groups of the current user, and see if there's the group you need:

function enableOrDisableAccordionTabs() {
  //first disable it here, outside of the loop
  pnp.sp.web.currentUser.groups().then(function(groups){
    for(var i = 0; i < groups.length; i++){
      if(group[i].Title == 'Supervisors'){
        //then enable it here, if the group you need is found
      }
    }
  });
}

MS365 groups are even harder to handle, might only be possible with graph API - groups - PnP/PnPjs

What am I missing?

fd.spRendered(function() {

function enableOrDisableAccordionTabs() {
//first disable it here, outside of the loop
}

fd.container('Accordion2').$children[0].disabled = true;

pnp.sp.web.currentUser.groups().then(function(groups){
for(var i = 0; i < groups.length; i++){
if(group[i].Title == 'ExamTeam'){

  }   else  {

// Enable Staff Only Accordion
fd.container('Accordion2').$children[0].disabled = false;

  }
}

enableOrDisableAccordionTabs();

  });

});

1 Like

Hello @shedev,

The code is outside of the function/ I've updated the code for you:

fd.spRendered(function() {
    //create function
    function enableOrDisableAccordionTabs() {
    //first disable it here, outside of the loop
        fd.container('Accordion2').$children[0].disabled = true;
    
        pnp.sp.web.currentUser.groups().then(function(groups){
        for(var i = 0; i < groups.length; i++){
        if(group[i].Title == 'ExamTeam'){
        
          }   else  {
        // Enable Staff Only Accordion
        fd.container('Accordion2').$children[0].disabled = false;
        
          }
        }
        });
    }
    
    //call function on form load
    enableOrDisableAccordionTabs();


});

Thank you @mnikitina - I have put this in place and still, members in the SharePoint 'ExamTeam' permissions group cannot see the 'Accordion2' and its contents. I will review more but in all tests, 'Accordion2' remains hidden.

@shedev,

To enable tab for the ExamTeam, you must move the action to the appropriate condition. And tehre is also a type error in the code. I've updated it:

    function enableOrDisableAccordionTabs() {
    //first disable it here, outside of the loop
        fd.container('Accordion2').$children[0].disabled = true;
    
        pnp.sp.web.currentUser.groups().then(function(groups){
        for(var i = 0; i < groups.length; i++){
        //was group instead of groups
        if(groups[i].Title == 'ExamTeam'){
                // Enable Staff Only Accordion
                fd.container('Accordion2').$children[0].disabled = false;
          }   
        }
        });
    }