Search code examples
javascriptdynamics-crmdynamics-crm-2016

How to check user privilege at runtime using javascript in Dynamics CRM 2016 v8.2?


I need to check the user privilege at run time.
Scenario: User1 created the record and it assigned to user2 for user1 record will be readonly now. So user1 does not have the access for that particular record. So can we check the runtime privilege for user1?


Solution

  • I know this is a followup question of this and you need a workaround to identify the Form mode - when user has Read privilege more than User level but Edit privilege on User level, as the CRM 2016 bug unable to classify the form type like Read-only vs Update.

    You have options like, choosing a field in the form to retrieve the privilege of that particular field for canRead, canUpdate access levels & decide what you want to do.

    Xrm.Page.getAttribute("field_name").getUserPrivilege();
    

    I think it is a bug, as it was working as expected from CRM 2011 until CRM 2015. The workaround we used is that, if the form type is 2, we are making another JavaScript Ajax call to check the user permission on the record, if it is read, then we consider the form is read-only, if it is write, then the form is in update mode.

    Of course, this solution adds a performance cost to the form load, but we find it good for our case.

    Another workaround, I think I saw it in StackOverflow, was that you looking for a field on the form that you know it should be editable in the write mode, and check if it is editable or read-only, but we didn't try this solution.

    Reference