Sunday, 23 October 2016

Show-Hide Section on the Basis of Security Role of Current User Using Javascript in MS Dynamics CRM

Show-Hide Section on the Basis of Security Role of Current User Using Javascript in MS Dynamics CRM
Show Fields on the Basis of Security Role of Current User Using Javascript in MS Dynamics CRM

Sometimes, we face such situation where we want to show some specific section to users having a specific role only.

Scenario: Let's say on case form you want to show a "Details" section under "general" tab only to the user having "CSR Manager" role.

Now,it can be achieved using Javascript easily using below code:

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Function Show-Hide a section based on a particular Role
function ShowHideSectionAccordingToRole()
{

if(CheckCurrentUserRole("CSR Manager"))
{
//Show the section
ToggleSection("general","Details",true);
}
else 
{
//Hide the section
ToggleSection("general","Details",false);
}
}

//Function to Show-Hide a section in Specific Tab
//Parameters
//TabName - Name of the Tab in Which the Section Exist
//SectionName - Name of the Section you want to Show-Hide
//Flag - true to Show and false to hide section
function ToggleSection(TabName, SectionName, Flag)
{       
    Xrm.Page.ui.tabs.get(TabName).sections.get(SectionName).setVisible(Flag);
}


//Check login User has role passed through Parameter
//Parameter
//IsRole - Name of the Role you want current user to check for
//Return Type - Boolean, True if User have that Role ,False if user doesn't have that Role
function CheckCurrentUserRole(IsRole) {
    var currentUserRoles = Xrm.Page.context.getUserRoles();
    for (var i = 0; i < currentUserRoles.length; i++) 
{
         var userRoleId = currentUserRoles[i];
         var userRoleName = GetRoleName(userRoleId);
         if (userRoleName == IsRole) 
   return true;
    }
    return false;
}

//Get Role-name based on RoleId
function GetRoleName(roleId) {
var serverUrl = Xrm.Page.context.getClientUrl();
var odataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc" + "/" + "RoleSet?$filter=RoleId eq guid'" + roleId + "'";
    var roleName = null;
    $.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) {
                roleName = data.d.results[0].Name;
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + textStatus + errorThrown + odataSelect); }
        }
    );
    return roleName;
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



Show Fields on the Basis of Security Role of Current User Using Javascript in MS Dynamics CRM

If you want to show/Hide or Lock/Unlock specific field instead of a whole section you can just replace the "ShowHideSectionAccordingToRole" function with the below function :

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function ShowHideFieldAccordingToRole()
{

if(CheckCurrentUserRole("CSR Manager"))
{
//Show the Field
Xrm.Page.ui.controls.get("FieldName").setVisible(true);
               //To UnLock the field
              // Xrm.Page.getControl("FieldName").setDisabled(false); 
}
else 
{
//Hide the Field
Xrm.Page.ui.controls.get("FieldName").setVisible(false);
               //To lockLock the field
              // Xrm.Page.getControl("FieldName").setDisabled(true); 
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Some may argue that why would we use javascript to show/hide field when we have field security profiles.
I agree we have field security profiles which have much more feature to control over a field but here Javascript  is also an easy way to handle this.

I hope it would be helpful. 
Comments are highly appreciated.

Happy CRM...!!!

No comments:

Post a Comment