sp-dev-docs icon indicating copy to clipboard operation
sp-dev-docs copied to clipboard

SharePoint Framewrok Extension Command Bar Button Visibility

Open enti333 opened this issue 7 years ago • 7 comments

  • [X ] Question

Hello. I want to as if is Possible the check if is user in group before custom button will be displayed. So I need to set visibility to true only if user is in specific group.

Thank you

enti333 avatar Jan 23 '19 15:01 enti333

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

msft-github-bot avatar Jan 23 '19 15:01 msft-github-bot

So that will be somewhat tricky, but here is what I would suggest.

In the onInit() function, make your rest call to get the group membership you are interested in (it would be easier if you could just use the permissions of the current user, but understand that might not work).

Store the value in a variable

In the call to see if a button should be visible, check your variable, and respond accordingly.

patmill avatar Jan 24 '19 18:01 patmill

Hello. Thank you very much for your suggestion. The problem is that the response is not so quick Here is my code:


@override
  public onInit(): Promise<void> {

    this.properties.email = this.context.pageContext.user.email;

    sp.web.siteGroups.getByName("test Members").users.getByEmail(this.properties.email).get().then(rs => {
      this.properties.isAdmin = true;
    }).catch(error => {
      this.properties.isAdmin = false;      
    });

    Log.info(LOG_SOURCE, 'Initialized DataImportCommandSet');
    return Promise.resolve();
  }

  @override
  public onListViewUpdated(event: IListViewCommandSetListViewUpdatedParameters): void {
    const compareOneCommand: Command = this.tryGetCommand('DATAIMPORT');
    
    if (compareOneCommand) {

      if(this.properties.isAdmin)
        compareOneCommand.visible = true;
      else
        compareOneCommand.visible = false;
    }
  }

Sometimes checking if user is in group is slower that checking the property....

Yes I can do something like this:

@enti333 override
  public onInit(): Promise<void> {

    this.properties.email = this.context.pageContext.user.email;
    
    Log.info(LOG_SOURCE, 'Initialized DataImportCommandSet');
    return Promise.resolve();
  }

  @override
  public onListViewUpdated(event: IListViewCommandSetListViewUpdatedParameters): void {
    const compareOneCommand: Command = this.tryGetCommand('DATAIMPORT');
    
    if (compareOneCommand) {

      sp.web.siteGroups.getByName("test Members").users.getByEmail(this.properties.email).get().then(rs => {
        compareOneCommand.visible = true;
      }).catch(error => {
        compareOneCommand.visible = false;
      });
    }
  }

The problem is that onListViewUpdated is called at least 4 times.... And this is not effective.... Hmm....Please what you think maybe some better solutions for this... Thank you.

enti333 avatar Jan 24 '19 19:01 enti333

Right - you want to have the query done once during the init phase, not on every update. What is the problem in the first sample?

patmill avatar Jan 25 '19 18:01 patmill

Hello. The problem is the query is asynch...That means it is "not so quick" to get this info....So the function onListViewUpdated is called earlier then I get result from Oninit function....So the button is everytime hidden. Hmm...Thank you for your help

enti333 avatar Jan 26 '19 09:01 enti333

@enti333 have you tried hiding in the onInit?

deanbot avatar Feb 22 '19 19:02 deanbot

This issue is being closed as part of an issue list cleanup project. Issues with no activity in the past 6 months that aren't tracked by engineering as bugs were closed as part of this inititive. If this is still an issue, please follow the steps outlined to re-open the issue.

msft-github-bot avatar Jan 22 '20 21:01 msft-github-bot