SharePoint Framewrok Extension Command Bar Button Visibility
- [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
Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.
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.
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.
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?
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 have you tried hiding in the onInit?
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.