Search This Blog

Thursday, May 16, 2013

MS CRM - Hide a form element based on the Users' Role

Often if testing a new element in CRM - for example an external link, you will only want it visible to certain users.
This may be the case even after that particular item is complete.
You can use javascript to do this, by querying for the user roles in Xrm.Page.context.getUserRoles():


function Form_onload()
{

    // check for test Role: 7e15636f-ab5f-e211-96b4-000c29f98cb8
    var testRoleId = '7e15636f-ab5f-e211-96b4-000c29f98cb8';
    var userRoles =Xrm.Page.context.getUserRoles();

    //test code  list user roles to console
    for (var i=0; i < userRoles.length; i++) {
        window.console &&  window.console.log(i + ' role:' + userRoles[i]);
    }

    var inTestRole =  userRoles.indexOf(testRoleId);
    if ( inTestRole < 0 ) {
        var navPriceListItem = document.getElementById('navLink{814d1cba-5412-2bc0-a7ae-46bc80384583}');
        navPriceListItem.style.display = 'none';
    }
}

Ideally you should be able to query the roles by name and also identify the form element better.


No comments:

Post a Comment