In this recipe, we will be looking at dynamically removing a ribbon button when a certain condition is met. First off, we'll create a function to check for the condition. If the condition is met, a second function is called that hides an existing button. So let's get started.
For this recipe we can either use an existing solution, or create a new one. Make sure you have permissions to make customizations in the system.
For the sake of simplicity, we check to see if the current user is the owner of a contact, and if not, we hide the Save button. You can do the same with the Save and Close button, as well as any other ribbon buttons.
function HideButtonIfNotOwner(context) { var _currentUser = Xrm.Page.context.getUserId(); var _formOwner; var _owner = Xrm.Page.getAttribute("ownerid"); if(_owner != null) { var _ownerValue = _owner.getValue(); if(_ownerValue != null) { var _formOwner = _ownerValue[0].id; } } if(_currentUser != _formOwner) { // hide ribbon button HideButton("contact|NoRelationship|Form|Mscrm.Form.contact.Save-Large"); // Save } }
HideButton()
function. In order to retrieve the names of the buttons, use the Internet Explorer's Developer Tools. You can open it by pressing the F12 key.function HideButton(name) { if(top.document.getElementById(name) != null) { var btn = top.document.getElementById(name); btn.style.display = 'none'; } }
HidebuttonIfNotOwner()
function to the OnLoad
event.Our first function retrieves the ID of the current user, as well as the ID of the form owner. Comparing these values, it defines whether to call the next function or not. In our case, if the owner is not the same as the current user, we are calling the next function to hide the Save button. In order to make this functional, you should also hide the save & Close button, as well as the Save & New button. Alternatively, you can hide the whole Save ribbon tab.
Our next function takes advantage of the standard HTML DOM to find the document elements described by the passed IDs and defines the style properties to hide them.
Same approach can be taken to hide ribbon tabs, not just buttons. Using the Developer Tools provided with Internet Explorer, you can find the ID of any form element, and hide it that way.
While this example only looked at the owner of a record versus the current user, you can easily extend this kind of functionality to enhance the process of progressing a certain record through various phases. A combination of a new custom ribbon tab with buttons to progress from one stage to another, along with form validation that defines the current stage, and what buttons become available next is something relatively easy to accomplish now using the previous examples provided.
3.15.188.27