Dynamics 365 CE: Hide Related Tab based on Security Role
Related tab in the Model Driven Apps (Unified Interface) is shown on every Main form. User can navigate to related items like Activities, Connection etc. using this tab.
We can hide this tab by un-checking “Show navigation items” property under Display tab of a form properties.
What if, we need to show this tab only for certain users – based on the security role. This tab is not available in the formContext.ui.tabs, hence we cannot hide it using setVisible() function of a tab.
But we have a trick, lets try it!
Problem
Hide Related tab based on Security Role (using JavaScript)
Solution
We can’t hide this tab, but we can hide Navigation Items under Related tab, this means the tab will still show, but there won’t be any related items available.
Here is the high-level logic:
- Register an onLoad function on the form.
- Get User’s role and implement conditional logic.
- Loop through Navigation Item collection and hide all related tabs.
Here is the sample code:
// Register on Load of the Form, pass execution context as first parameter
function onLoad(executionContext) {
// Get Form Context
const formContext = executionContext.getFormContext();
// Get Navigation collection
const navigations = formContext.ui.navigation.items;
// Get User Roles
const userRoles = Xrm.Utility.getGlobalContext().userSettings.roles;
// Verify if User is Admin
const isAdmin = isAdminRole();
// If not Admin
if (!isAdmin) {
// Hide all related tabs
navigations.forEach(function(item) {
item.setVisible(false);
});
}
}
// Verify if User has a role named "System Administrator"
function isAdminRole() {
const roles = Xrm.Utility.getGlobalContext().userSettings.roles;
if (roles === null) return false;
var hasRole = false;
roles.forEach(function (item) {
if (item.name.toLowerCase() === "system administrator") {
hasRole = true;
}
});
return hasRole;
}
In above code, we loop through formContext.ui.navigation.items collection, and hide all nav. items using setVisible function.
Result
For Non-Admin Users, Related tab has no options.

However for the Admins, Related tab shows all options:

That’s it, Quite Easily Done!
This was originally posted here.

Like
Report
*This post is locked for comments