I have Tab on d365 form , which I have to control using security roles ,
How can I apply security role to Tab?
Thanks,
Shafqat
Hi @Shafdev
You can use the below code to achieve the same
function hideTabBasedOnRole(tabName, requiredRoleName) {
var currentUserRoles = Xrm.Utility.getGlobalContext().userSettings.roles;
var hasRequiredRole = false;
for (var i = 0; i < currentUserRoles.getLength(); i++) {
var userRole = currentUserRoles.get(i).getName();
if (userRole === requiredRoleName) {
hasRequiredRole = true;
break;
}
}
if (!hasRequiredRole) {
var tabToHide = Xrm.Page.ui.tabs.get(tabName);
if (tabToHide) {
tabToHide.setVisible(false);
} else {
console.error("Tab '" + tabName + "' not found.");
}
}
}
Call the above function with
hideTabBasedOnRole("tab_details", "Administrator");
You can use my video for how to write JS (https://www.youtube.com/watch?v=x8kYNCkikgs)
Please mark as Answer if it is helpful and provide Kudos
Subscribe : https://www.youtube.com/channel/UCnGNN3hdlKBOr6PXotskNLA
Follow me on Twitter : @rampprakashd
Blog : https://microsoftcrmtechie.blogspot.com
Hi @Shafdev
You can use a function like this
function hideTabBasedOnRole() {
var currentUserRoles = Xrm.Utility.getGlobalContext().userSettings.roles;
var isAdmin = false; // Assuming a specific role is required for access
for (var i = 0; i < currentUserRoles.getLength(); i++) {
var roleName = currentUserRoles.get(i).getName();
if (roleName === "Administrator") {
isAdmin = true;
break;
}
}
if (!isAdmin) {
Xrm.Page.ui.tabs.get("your_tab_name").setVisible(false); // Hide the tab
}
}
hideTabBasedOnRole();
If I have answered your question, please mark your post as Solved.
If you like my response or got a direction forward to proceed, please give it a Thumbs Up. Appreciate your Kudos. You can accept more than one post as a solution.
If you were still facing any issue or challenge, please do let me know so that I can try to check for you. Happy to help you further.
Cheers,
PMDY
Hi,
You need to write script to get current user security roles and based on the security roles you can hide/show tab.
Here is the example with more details : https://ajitpatra.com/2020/09/11/d365-ce-get-logged-in-users-security-roles-using-javascript/
https://crmminds.com/2022/10/10/dynamics-365-how-to-show-hide-sections-and-form-tabs/
WarrenBelz
637
Most Valuable Professional
stampcoin
570
Super User 2025 Season 2
Power Apps 1919
473