Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

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

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.

  • Suggested answer
    MehranBre Profile Picture
    MehranBre 730 on at
    RE: Show "SP documents Subgrid" based based on security role?

    your Welcome,

    please verify my answer if this helped.

    Best Regards.

  • nidnani Profile Picture
    nidnani 285 on at
    RE: Show "SP documents Subgrid" based based on security role?

    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. :) 

  • Verified answer
    MehranBre Profile Picture
    MehranBre 730 on at
    RE: Show "SP documents Subgrid" based based on security role?

    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
    nidnani 285 on at
    RE: Show "SP documents Subgrid" based based on security role?

    Hi Charles, can you please share the code? thanks

  • Suggested answer
    Charles Abi Khirs Profile Picture
    Charles Abi Khirs 3,569 on at
    RE: Show "SP documents Subgrid" based based on security role?

    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
    nidnani 285 on at
    RE: Show "SP documents Subgrid" based based on security role?

    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
    MehranBre Profile Picture
    MehranBre 730 on at
    RE: Show "SP documents Subgrid" based based on security role?

    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
    nidnani 285 on at
    RE: Show "SP documents Subgrid" based based on security role?

    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
    Inogic Profile Picture
    Inogic 23,963 on at
    RE: Show "SP documents Subgrid" based based on security role?
    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
    nidnani 285 on at
    RE: Show "SP documents Subgrid" based based on security role?

    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.

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,696 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,490 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans