How to control command visibility in response to "external" conditions
Category
- [x] Question
- [ ] Typo
- [ ] Bug
- [ ] Additional article idea
I am developing a ListView Command Set extension and want to be able to control the visibility of commands based on a condition that can be changed at any point in time. From my experience with SPFx it is only possible to control command visibility by directly setting it in onListViewUpdated. Trying to set command visibility in other methods (e.g. onExecute) has no effect. How can I react to changes and set command visibility in response to changes in a condition? Please let me on how to accomplish this. Is there a way to trigger onListViewUpdated so that command visibility can be changed?
Try binding your visibility to state variables, and call setState(). It usually works for me.
Closing based on Hugo's response. Let's reopen if needed.
@hugoabernier first of all, many thanks for your answer. Sadly, I am not sure I understand how it applies to my problem. Following is an attempt to clarify my question:
Say I need to query a web API to determine if a command should be visible.
- I start by obtaining the command by calling this.tryGetCommand.
- Then, I call an async method passing it the command.
- Inside the async method, I update command visibility according to the response from the web API. However, changing the visible property of the command does not force a redraw of the UI.
- When at a later time the onListViewUpdated method is called, the updated command visibility will be reflected in the UI.
Some pseudocode describing how I do the above:
@override
public onListViewUpdated(event: IListViewCommandSetListViewUpdatedParameters): void {
const command: Command | undefined = this.tryGetCommand("COMMAND_ID"); // 1
this.updateVisibility(command); // 2
}
private updateVisibility(command: Command): Promise<void> {
// call web API to determine if command should be visible
command.visible = resultFromApiCall; // 3
}
Your answer (as I understand it) is based on react components and I do not understand how it applies here. I would be very grateful if you could elaborate your answer and/or provide a code example to help me understand what I am missing.
Did you find any solution to this @Qvintus2 ? I have the exact same challenge now. @VesaJuvonen I think you should reopen this issue as @hugoabernier`s answer isnt really the solution to this problem.
Command set buttons does not update when running async functions. This is an important feature as we often would want to show and hide buttons based on response from the SharePoint API.
@VesaJuvonen I believe this should be re-opened. @hugoabernier, there is no setState function in a listview command set, as BaseListViewCommandSet is not a React component.
Add for example
setTimeout(() => { moveCommand.visible = false; }, 5000);
to onListViewUpdated and you will see it has no effect. Visibility must be changed while the method runs, and not later. If onListViewUpdated had been marked as being async, it would make life easier I guess :)
Working on a solution where I have to retrieve column values for the selected item as it's not in the default view, and then check if the command should show or not.
Hi All,
I had this same issue. I know it's not a complete solution, but I noticed the OnInit function returns a promise. Use this to execute any async function and resolve once you've got the value, then use the cached value during the onListViewUpdated to set the button visibility.
Works for retrieving properties, like if the current view is the default or not. But will only work on the page load. Not sure how to force a re-render to handle a situation where you want to push an update to the property.
@override
public onInit(): Promise<void> {
Log.info(LOG_SOURCE, 'Initialized VitgSpFormsActionsCommandSet');
var promise = new Promise<void>((resolve, reject) => {
this.getSomeApiProperty().done(function() {
resolve();
})
});
return promise;
}
@aronbardsley not really an option to preload data for an arbitrary number of files in a library. I went with always showing the button and perform the logic on click and display a dialog, like for classic ribbon actions. Not ideal, but a workaround.
Hello. Please any updates to this problem. It is not possible to make some better logic for command bar buttons right now. Thank you.
Hi, exact same issue. It would be great to be able to show/hide command buttons based on some backend api response, say by passing context based on a list item selection.