Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
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 24,094 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.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans