Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Hide Related tab in Contact based on security role

(4) ShareShare
ReportReport
Posted on by 5,382
Hi All,
 
I have a client requirement where
I need to hide a RELATED tab in contact form if the user is not assigned with "d365_business user " and "d365_businesssupport" security role.
si this achievable if yes how?
 
 
 
 
 
Thanks
Sandeep
Categories:
  • Suggested answer
    Tom_Gioielli Profile Picture
    1,358 on at
    Hide Related tab in Contact based on security role
    You can hide entries on the related tab, but it cannot be done dynamically by checking security role (the exception to this is if the users do not have access to the related table without the above roles, then the entry will not appear).
     
    I might recommend the following to meet the requirement.
    • Remove the table from the Related tab (I'm 90% sure you can do this in the new maker portal, but you can definitely do it in the classic version)
    • Add a new tab onto the form with the name of this related table and add a subgrid to show the records. Basically, take it out of the related section and add it straight to your form
    • User JavaScript to check user security role associations on load of the form, and hide/show the tab with your records dynamically based on those results
     
    I will add that it may be possible to access the "Related" tab and the menu options through JavaScript directly (and thus bypass steps 1 and 2 above), but I am not sure if it can be done or how it would be done. The code listed in a response below will not work, because the related section is not a traditional tab and thus cannot be access through that method. as far as I know.
  • sandeepc Profile Picture
    5,382 on at
    Hide Related tab in Contact based on security role
    CRM sales
  • sandeepc Profile Picture
    5,382 on at
    Hide Related tab in Contact based on security role
     
    I think related tab is not general tab to have tab name on form. i cannot see related tab in backend. i need to hide this related tab
  • Verified answer
    Daivat Vartak (v-9davar) Profile Picture
    5,970 Super User 2025 Season 1 on at
    Hide Related tab in Contact based on security role
    Hello Sandeep,
     
    Yes, this requirement is absolutely achievable in Dynamics 365 using JavaScript and web resources.
     
    Here's a step-by-step guide on how to hide a related tab on the Contact form based on user security roles:
    1. Identify the Tab and Security Roles:
    • Tab Name: Determine the exact name of the related tab you want to hide. You can find this by opening the Contact form editor and looking at the tab's properties.
    • Security Roles: Confirm the exact names of the security roles ("d365_business user" and "d365_businesssupport") that should have access to the tab.
    2. Create a JavaScript Web Resource:
    • Create a New Web Resource:
      • In Dynamics 365, go to Settings > Customizations > Customize the System.  
      • Expand Web Resources and click New.
      • Display Name: Give the web resource a descriptive name (e.g., "HideRelatedTabBasedOnRole").
      • Name: Give the web resource a schema name that is unique.
      • Type: Select "Script (JScript)".
      • Text Editor: Click the "Text Editor" button.
    • Add JavaScript Code:
      • Paste the following JavaScript code into the text editor:
     
    JavaScript (Example)
     
        function hideRelatedTabBasedOnRole(executionContext) {
            var formContext = executionContext.getFormContext();
            var userRoles = Xrm.Utility.getGlobalContext().userSettings.securityRoles;
            var tabName = "YOUR_RELATED_TAB_NAME"; // Replace with your tab name
            var allowedRoles = ["d365_business user", "d365_businesssupport"];

    var userHasAllowedRole = false;
            for (var i = 0; i < userRoles.length; i++) {
                var roleName = Xrm.Utility.getGlobalContext().securityRoles.getById(userRoles[i]).name;
                if (allowedRoles.indexOf(roleName) !== -1) {
                    userHasAllowedRole = true;
                    break;
                }
            }

    if (!userHasAllowedRole) {
                formContext.ui.tabs.get(tabName).setVisible(false);
            }
        }

     
    • Replace Placeholder:
      • Replace "YOUR_RELATED_TAB_NAME" with the actual name of your related tab.
    • Save and Publish:
      • Click OK to close the text editor.
      • Save and then Publish the web resource.
    3. Add the Web Resource to the Contact Form:
    • Open the Contact Form Editor:
      • In Dynamics 365, go to Settings > Customizations > Customize the System.  
      • Expand Entities > Contact > Forms.
      • Open the Contact form you want to modify.
    • Add the Web Resource:
      • In the form editor, click Form Properties.
      • Go to the Events tab.
      • In the Form Libraries section, click Add.
      • Select the web resource you created ("HideRelatedTabBasedOnRole").
      • Click Add.
    • Add the OnLoad Event Handler:
      • In the Event Handlers section, click Add.
      • Event: Select "OnLoad".
      • Library: select the web resource you added.
      • Function: Enter hideRelatedTabBasedOnRole.
      • Check the "Pass execution context as first parameter" checkbox.
      • Click OK.
    • Save and Publish:
      • Click OK to close the Form Properties dialog.
      • Save and then Publish the Contact form.
     
    Explanation:
    • Xrm.Utility.getGlobalContext().userSettings.securityRoles: This retrieves an array of the user's security role GUIDs.  
    • Xrm.Utility.getGlobalContext().securityRoles.getById(userRoles[i]).name: This retrieves the name of a security role based on its GUID.
    • formContext.ui.tabs.get(tabName).setVisible(false): This hides the specified tab.
    • The code loops through the users security roles, and checks if any of the users roles match the allowed roles. If the user does not have an allowed role, then the tab is hidden.
     
    Important Notes:
    • Tab Name: Make sure you enter the correct tab name in the JavaScript code.
    • Security Role Names: Ensure the security role names are spelled correctly and match the exact names in Dynamics 365.
    • Testing: Thoroughly test the functionality with different users and security roles.
    • Publishing: Always remember to publish your changes after making modifications.
    • Performance: For complex forms with many security role checks, consider optimizing your JavaScript code for performance.
     
    If my answer was helpful, please click Like, and if it solved your problem, please mark it as verified to help other community members find more.
    If you have further questions, please feel free to contact me.
     
    My response was crafted with AI assistance and tailored to provide detailed and actionable guidance for your Microsoft Dynamics 365 query.
     
    Best Regards,
    Daivat Vartak
  • Ramesh Kumar Profile Picture
    3,280 on at
    Hide Related tab in Contact based on security role
    Can you please help provide which application or ERP we are talking about
     
    Thanks
    Ramesh
  • Verified answer
    Dharanidharan Profile Picture
    621 User Group Leader on at
    Hide Related tab in Contact based on security role
    Hi Sandeep,
     
    Yes, this is achievable using JavaScript in the contact form. You can check the user's security roles on form load and hide the RELATED tab if they do not have the required roles.
    Here's a sample JavaScript function to achieve this:
    function hideRelatedTabBasedOnSecurityRole(executionContext) {
        var formContext = executionContext.getFormContext();
        var userRoles = Xrm.Utility.getGlobalContext().userSettings.roles;
        var requiredRoles = ["d365_business user", "d365_businesssupport"];
        
        var hasRequiredRole = userRoles.some(function (role) {
            return requiredRoles.includes(role.name);
        });
    
        if (!hasRequiredRole) {
            formContext.ui.tabs.get("tab_name").setVisible(false);
        }
    }
    
    Steps to Implement:
    1. Replace "tab_name" with the actual logical name of the RELATED tab.
    2. Add this function to the OnLoad event of the contact form in PowerApps.
    3. Ensure the web resource containing this script is published and associated with the form.
    Let me know if you need further clarification.
  • André Arnaud de Calavon Profile Picture
    294,095 Super User 2025 Season 1 on at
    Hide Related tab in Contact based on security role
    Hi Sandeep,

    Can you tell us what Dynamics 365 application this question is related to? There are several different solutions in the Dynamics 365 family, like: Sales, Finance, Supply Chain Management, Project Operations, and Business Central. If you can clarify the product, we can move the question to the dedicated forum for that solution and help you with the correct answer.

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,095 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,866 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans