Recently someone posted in developer community on how to hide some menu item from command bar in Modern Experience. In this article, we will see how this can be achieved via SPFx extension application customizer.
I am assuming that you already know how to create a hello world SPFx extension, if not please go through this link.
Once you have your hello world spfx extension ready and running, let us understand how these command bar menu items are structured in HTML. For this article, we will hide a New button from this command bar.
Below is a screenshot of command bar in a typical document library, more or less it is the same for list also.

Use F12 developer console and inspect New button element, you would see something like below,

We will see that name attribute is something which is unique and can be used to identify element and apply any JavaScript code.
Let us now go to hello world extension code, open it in your editor and locate ApplicationCustomizer.ts(it would be different in your case).
Find onInit () method, add below code in this method,
- @override
- public onInit(): Promise<void> {
- Log.info(LOG_SOURCE, `Initialized ${strings.Title}`);
- let message: string = this.properties.testMessage;
- if (!message) {
- message = '(No properties were provided.)';
- }
- Dialog.alert(`Hello from ${strings.Title}:\n\n${message}`);
- // code to hide button
- let newbutton: any = document.getElementsByName("New")[0] || document.documentElement;
- Dialog.alert(newbutton.name);
- newbutton.style.display = "none";
- return Promise.resolve();
- }
- }
Use 'gulp serve' again and preview your page(pageUrl configured in serve.json) - we can see New button is hidden.

Similarly, you can hide other menu items, use the chrome developer console to find value of name attributes.
Important points
- We can restrict this extension to a particular list/library or content type. Use custom action RegistrationType or RegistrationId to apply custom action to particular content.
- Maybe in future updates, there can be a change in name of these attributes and its values, so this needs an eye check every while and then.
- We can make this dynamic by passing parameters to extension from custom action on which all buttons need to be hidden.
- We can also use jquery and apply hierarchical based filter on elements and then apply customization.
Hope this helps ... happy coding!!!

Harry ChenPosted Sep 11, 2020, 4:00 PM
This is working when first displayed the list. After user select one item and de-select it, the hidden "New" button shows again. Is there anyway to resolve this issue? Thanks.
legendgod legendgodPosted Sep 10, 2020, 2:19 AM
Could you show us how to handle nested JSON respond based on this example? Thank you
shubham MATHURPosted Jul 16, 2020, 7:17 AM
Thanks. BUt it is working fine for New,Download all button which are already exist in Command Bar before selecting any item in a Library. I need to hide Delete button which visible after selecting any item. First time when i select any item, it is showing undefined value.
Amit KumarPosted Jul 16, 2019, 10:24 PM
This is not working in IE