Security trimmed ribbon elements

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.

Getting ready

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.

How to do it...

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.

  1. Create a new solution or open an existing one.
  2. Add the Contact entity to your solution of not already added.
  3. Add a new web resource of type Jscript.
  4. Add the following function to check if the current user is the owner of the record:
    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
    }
    }
  5. Observe the button name that is being passed to the 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.
  6. Next, add a new function that hides the button using the HTML DOM. Please note that while this is not an officially supported scenario, as long as the HTML DOM does not change, it's a simple way of doing this.
    function HideButton(name)
    {
      if(top.document.getElementById(name) != null)
      {
        var btn = top.document.getElementById(name);
        btn.style.display = 'none';
      }
    }
  7. Open the Contact form, and in the form properties, add the HidebuttonIfNotOwner() function to the OnLoad event.
  8. Save and Publish your solution.
  9. Test your customization by opening a record you own. You should see a Save button.
    How to do it...
  10. Next test it by opening a record where the owner is another system user. You should not see the Save button anymore.
    How to do it...

How it works...

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.

There's more...

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.

Enhancing the progression process of a record

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.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.188.27