web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Hide a custom ribbon button with JavaScript

(0) ShareShare
ReportReport
Posted on by 12
I need to hide a button in ribbon with javascript(without ribbon workbench),
I am trying to get the button with it's arial-label dynamic, because I need to be sure my code works well in all our environments.
I use this code, but it doesn't work:
even if I eliminate addeventlistener(), the buttoncontrols always return null.
 
function HideInfoButton(executionContext) {
    // Wait for the DOM to be fully loaded
    document.addEventListener('DOMContentLoaded', function () {
      var formContext = executionContext.getFormContext(); // Get the form context
 
      // Use querySelectorAll to get all elements with data-id attribute
      var buttonControls = formContext.ui.controls.get(
          function (control) {
              // Replace 'aria-label' and 'To update' with the actual attribute and value
              return control.getAttribute().getName() === 'aria-label' && control.getAttribute().getValue() === 'Aktualisieren';
          }
      );
 
      if (buttonControls.length > 0) {
          console.log('Button will be hidden');
          buttonControls.forEach(function (buttonControl) {
              buttonControl.setVisible(false); // Hide each button
          });
      }
  });
 
}
 
 
I have the same question (0)
  • Suggested answer
    Dengliang Li Profile Picture
    Microsoft Employee on at
    Hi,
     
    According to the official documentation, the type of control does not include buttons.
     
    You need to get the HTML element of the button through the element selector.
    Then hide the button by setting the button: style.display = "none".
     
    Best Regards,
    Dengliang Li
     
  • SB-28050802-0 Profile Picture
    12 on at
    Thanks Dengliang Li,
     
    So I've changed it to this but it doesn't work. queryselector can not find the element, even with or without button[].
  • Verified answer
    Dengliang Li Profile Picture
    Microsoft Employee on at
    Hi,
     
    You need to precede document.queryselector with /window.top./ i.e. window.top.document.queryselector.
    Get the button's HTML element in the top browser context frame.
     
    Best Regards,
    Dengliang Li
     
     
  • SB-28050802-0 Profile Picture
    12 on at
    Finally I used this code and it works, but I always have to press+f5 to hide the button, it doesn't happen at first time.
    anybody has a suggestion?
     
    function HideRibbonButton() {
       
          var iAmInAFrame = (window.top !== window.self);
          const buttonControls = window.top.document.querySelector('button[aria-label="Info/Frage. Eine E-Mail wird an den hinterlegten Benutzer verschickt"]');
          console.log(buttonControls)  ;
     
          if (buttonControls!=null) {
              console.log("Button Info/Frage will be hidden");
              buttonControls.style.display= "none";
              };
             
      }  

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 61

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans