web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Show "SP documents Subgrid" based based on security role?

(0) ShareShare
ReportReport
Posted on by 285

Hi, we have a requirement to Show Documents Subgrid only to Customer Service and Hide for everyone else.

I am using the below code for both purposes. But it hides the Subgrid for CSR as well.

Below is the WebResource and Calling both the functions onLoad Page.

// Show SP Grid and Load Payment Tab for CS

var leadFormCustomization = {

customEditAccess: function (execContext) {
debugger;
var formContext = execContext.getFormContext();
var userSettings = Xrm.Utility.getGlobalContext().userSettings;

//Get Security Roles of the current User
var securityRoles = userSettings.securityRoles;

//Below is the GUID of the Security Role "CustomEditAccess"
var securityRoleId = "7AD8BA9C-7A04-E911-810D-0050569D9B94"; // CSR Manager

for (i = 0; i < securityRoles.length; i  ) {
//If current User contains the Required Security Role
if (securityRoles[i].toUpperCase() == securityRoleId.toUpperCase()) {
		
     formContext.getControl('Sharepoint_grid').setVisible(true); //Show Subgrid
     formContext.ui.tabs.get("tab_2").setFocus(); //Load Tab


}}}}

// Show Hide SharePoint Grid to Upload Documents

function ShowhideSubgrid2(executionContext) {
        var formContext = executionContext.getFormContext(); // get the form Context
        var uploadContract = formContext.getAttribute("rnu_uploadcontract_payment").getValue(); // get the option set value Yes
        if(uploadContract == 1)
       {
               formContext.getControl('Sharepoint_grid').setVisible(true); //Show Subgrid
       }
       else
       {     formContext.getControl('Sharepoint_grid').setVisible(false); //Hide Subgrid
       }
}

How can I avoid hiding it for the CSR role???

Thank you in advance.

I have the same question (0)
  • Suggested answer
    Charles Abi Khirs Profile Picture
    3,569 on at

    Hello,

    Which function are you using to hide the sub-grid, as per the code, there are 2 functions that manage the visibility of the sub-grid.

    In addition, try to put the sub-grid in a section and show/hide the section itself instead of the sub-grid. Here is a link to a similar thread : community.dynamics.com/.../705193

  • nidnani Profile Picture
    285 on at

    Hi Charles,

    I am calling the function ShowhideSubgrid2, to hide the subgrid for everyone when the page loads.

    And the first function is for the CSR Security role to display the subgrid when the page loads.

    It is already under a separate section, but there are few other fields in the same section which other users prefer to see.

  • Suggested answer
    Inogic Profile Picture
    709 on at
    Hi,
    In the code, I noticed you are showing the subgrid based on the security role but also have an additional logic to show/hide the same subgrid based on the value of a field.
    0412.image001.png
    In the above logic, you are only checking the uploadContract count and if it does not equal 1 then you are hiding the subgrid.
    I suspect first the function which based on the security role is working fine and is showing the subgrid but then the function ‘ShowhideSubgrid2’  is executed and is hiding the subgrid again.
    You can create a global var name ‘hasCSRSecurityRole’ and set it to true inside the security role condition check.
    And then in ‘ShowhideSubgrid2’ function check both condition i.e. hasCSRSecurityRole and uploadContract count.
    Could you please check if this is the case and also check if the subgrid name is correct?
    Thanks!
  • nidnani Profile Picture
    285 on at

    Hi Roohi,

    Yes, the subgrid name is correct.

    I verified and yes that's what is happening. The first function with the security role is showing the subgrid but the second function is hiding it.

    Could you please provide an example of the code for checking the security role and upload contract value both together? It would really help.

  • Suggested answer
    MehranBre Profile Picture
    732 on at

    Hi,

    Use only this function instead of the above two:

    // Show SP Grid and Load Payment Tab for CS
    
    var leadFormCustomization = {
    
        customEditAccess: function (execContext) {
    
            var formContext = execContext.getFormContext();
            var userSettings = Xrm.Utility.getGlobalContext().userSettings;
    
            //Get Security Roles of the current User
            var securityRoles = userSettings.securityRoles;
    
            //Below is the GUID of the Security Role "CustomEditAccess"
            var securityRoleId = "7AD8BA9C-7A04-E911-810D-0050569D9B94"; // CSR Manager
    
            var formContext = executionContext.getFormContext(); // get the form Context
            var uploadContract = formContext.getAttribute("rnu_uploadcontract_payment").getValue();
    
            for (i = 0; i < securityRoles.length; i  ) {
                //If current User contains the Required Security Role
                if (securityRoles[i].toUpperCase() == securityRoleId.toUpperCase() || uploadContract == 1) {
    
                    formContext.getControl('Sharepoint_grid').setVisible(true); //Show Subgrid
                    formContext.ui.tabs.get("tab_2").setFocus(); //Load Tab
                }
                else {
                    formContext.getControl('Sharepoint_grid').setVisible(false); //Hide Subgrid
                }
            }
        }
    }

    Best Regards.

  • nidnani Profile Picture
    285 on at

    Hi Mehran, I tried that but since Users with CSR managers also have "Customer Service App" security role. The script hides the Sharepoint grid for them as well. :(

    Any other way to avoid it? or am I something wrong?

  • Suggested answer
    Charles Abi Khirs Profile Picture
    3,569 on at

    Hello,

    Create a variable and define its value to false. In the for loop if condition, set the variable to true and break from the for loop. Outside the for loop check if the variable = true => show the grid, else => hide the grid.

  • nidnani Profile Picture
    285 on at

    Hi Charles, can you please share the code? thanks

  • Verified answer
    MehranBre Profile Picture
    732 on at

    hi, try this:

    // Show SP Grid and Load Payment Tab for CS
    
    var leadFormCustomization = {
    
        customEditAccess: function (execContext) {
    
            var formContext = execContext.getFormContext();
            var userSettings = Xrm.Utility.getGlobalContext().userSettings;
    
            //Get Security Roles of the current User
            var securityRoles = userSettings.securityRoles;
    
            //Below is the GUID of the Security Role "CustomEditAccess"
            var securityRoleId = "7AD8BA9C-7A04-E911-810D-0050569D9B94"; // CSR Manager
    
            var formContext = executionContext.getFormContext(); // get the form Context
            var uploadContract = formContext.getAttribute("rnu_uploadcontract_payment").getValue();
    
            for (i = 0; i < securityRoles.length; i  ) {
                //If current User contains the Required Security Role
                if (securityRoles[i].toUpperCase() == securityRoleId.toUpperCase() || uploadContract == 1) {
    
                    formContext.getControl('Sharepoint_grid').setVisible(true); //Show Subgrid
                    formContext.ui.tabs.get("tab_2").setFocus(); //Load Tab
                    break;
                }
                else {
                    formContext.getControl('Sharepoint_grid').setVisible(false); //Hide Subgrid
                }
            }
        }
    }

    Regards.

  • nidnani Profile Picture
    285 on at

    Thank you so much, Mehran. Adding a break to the code worked. Below is the final working updated code.

    var leadFormCustomization = {
    customEditAccess: function (execContext) {
    debugger;
    var formContext = execContext.getFormContext();
    var userSettings = Xrm.Utility.getGlobalContext().userSettings;
    
    //Get Security Roles of the current User
    var securityRoles = userSettings.securityRoles;
    
    //Below is the GUID of the Security Role "CustomEditAccess"
    var securityRoleId = "7AD8BA9C-7A04-E911-810D-0050569D9B94"; //Renuit CSR Manager
    
    //Below Get value of upload contract
    var uploadContract = formContext.getAttribute("rnu_uploadcontract_payment").getValue();
    
            for (i = 0; i < securityRoles.length; i  ) {
                //If current User contains the Required Security Role
                if (securityRoles[i].toUpperCase() == securityRoleId.toUpperCase() || uploadContract == 1)
    			{
                    formContext.getControl('Sharepoint_grid').setVisible(true); //Show Subgrid
                    formContext.ui.tabs.get("tab_2").setFocus(); //Load Tab
                    break;
                }
                else
    			{
                    formContext.getControl('Sharepoint_grid').setVisible(false); //Hide Subgrid
                }
              
            }
        }
    }
         

    Thanks a lot, everyone. It is working now. Learned a lot thanks again. :) 

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans