SBX - Search With Button

SBX - Forum Post Title

Based upon the security role, the user should be able to see the section on the entity form.

Microsoft Dynamics CRM Forum

Amit Kumar Rath asked a question on 13 Oct 2018 5:12 AM
My Badges

Question Status

Suggested Answer

Discussion:

I have a scenario where the user with security role A should be able to see the section "Account details" and the user with security role B should be able to see the section "Account forecasts". But the vice-versa is not possible.

What are the probable approaches to achieve it and how.

Reply
Temmy Raharjo responded on 13 Oct 2018 8:04 AM
My Badges
Suggested Answer

We don't have this feature out of the box function. You need to create custom function on javascript to retrieve if the current user have this sec-role. Then you need to set section visible to false.

Reply
Goutam Das responded on 13 Oct 2018 12:15 PM
Suggested Answer

Hi Amit,

There is only possibility to do this using Javascript -

Here is sample JS function , try with this . Register the function in form onload event.

function CheckSecurityRole() {
    //Get User Role Name
    var currentUserRoles = Xrm.Page.context.getUserRoles();    
    var hasRoleA = false;
    var hasRoleB = false;

    var serverUrl = Xrm.Page.context.getClientUrl();
    var odataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc" + "/";
    for (var i = 0; i < currentUserRoles.length; i++) {
        var userRoleId = currentUserRoles[i];
        if (i == 0)
            odataSelect = odataSelect + "RoleSet?$filter=RoleId eq guid'" + userRoleId + "'";
        else
            odataSelect = odataSelect + " or RoleId eq guid'" + userRoleId + "'";
    }
    $.ajax({
        type: "GET",
        async: false,
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: odataSelect,
        beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
        success: function (data, textStatus, XmlHttpRequest) {
            for (var j = 0; j < data.d.results.length ; j++) {
                var roles = data.d.results[j].Name;               
                if (roles.toUpperCase() == "ROLEA") { hasRoleA = true; } // Replace the Role Name which you want
                if (roles.toUpperCase() == "ROLEB") { hasRoleB = true; }// Replace the Role Name which you want
            }
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) { alert('ODataSelect Failed: ' + textStatus + errorThrown + odataSelect); }
    }
    );

    if (hasRoleA)
    {
        // Show hide  section
        Xrm.Page.ui.tabs.get("tabName").sections.get("Accountdetails").setVisible(true); // Replace the Tab Name and Correct Section Name
        Xrm.Page.ui.tabs.get("tabName").sections.get("Accountforecasts").setVisible(false);// Replace the Tab Name and Correct Section Name
    }  
    

    if (hasRoleB)
    {
        // Show Hide section      
        Xrm.Page.ui.tabs.get("tabName").sections.get("Accountdetails").setVisible(false);
        Xrm.Page.ui.tabs.get("tabName").sections.get("Account forecasts").setVisible(true);
    }
    
}

Reply
Aric Levin responded on 13 Oct 2018 11:40 PM

Hi Amit,

If Scripting is not the right solution for you, you can always create multiple forms (one for each security role), and assign the Security Role to the form.

That way Security Role A users will see form A, and Security Role B users will see form B.

Hope this helps.

Reply
Temmy Raharjo responded on 13 Oct 2018 8:04 AM
My Badges
Suggested Answer

We don't have this feature out of the box function. You need to create custom function on javascript to retrieve if the current user have this sec-role. Then you need to set section visible to false.

Reply
Goutam Das responded on 13 Oct 2018 12:15 PM
Suggested Answer

Hi Amit,

There is only possibility to do this using Javascript -

Here is sample JS function , try with this . Register the function in form onload event.

function CheckSecurityRole() {
    //Get User Role Name
    var currentUserRoles = Xrm.Page.context.getUserRoles();    
    var hasRoleA = false;
    var hasRoleB = false;

    var serverUrl = Xrm.Page.context.getClientUrl();
    var odataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc" + "/";
    for (var i = 0; i < currentUserRoles.length; i++) {
        var userRoleId = currentUserRoles[i];
        if (i == 0)
            odataSelect = odataSelect + "RoleSet?$filter=RoleId eq guid'" + userRoleId + "'";
        else
            odataSelect = odataSelect + " or RoleId eq guid'" + userRoleId + "'";
    }
    $.ajax({
        type: "GET",
        async: false,
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: odataSelect,
        beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
        success: function (data, textStatus, XmlHttpRequest) {
            for (var j = 0; j < data.d.results.length ; j++) {
                var roles = data.d.results[j].Name;               
                if (roles.toUpperCase() == "ROLEA") { hasRoleA = true; } // Replace the Role Name which you want
                if (roles.toUpperCase() == "ROLEB") { hasRoleB = true; }// Replace the Role Name which you want
            }
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) { alert('ODataSelect Failed: ' + textStatus + errorThrown + odataSelect); }
    }
    );

    if (hasRoleA)
    {
        // Show hide  section
        Xrm.Page.ui.tabs.get("tabName").sections.get("Accountdetails").setVisible(true); // Replace the Tab Name and Correct Section Name
        Xrm.Page.ui.tabs.get("tabName").sections.get("Accountforecasts").setVisible(false);// Replace the Tab Name and Correct Section Name
    }  
    

    if (hasRoleB)
    {
        // Show Hide section      
        Xrm.Page.ui.tabs.get("tabName").sections.get("Accountdetails").setVisible(false);
        Xrm.Page.ui.tabs.get("tabName").sections.get("Account forecasts").setVisible(true);
    }
    
}

Reply

SBX - Two Col Forum

SBX - Migrated JS