Check whether the current logged in Dynamics 365 CRM user has necessary security privileges for a specific entity in PCF Control

By | March 9, 2021

Introduction

In this blog, we will get the idea on how we can check if the current logged-in user has Read/Write/Update access to any specific entity or not.

Scenario

We have one Boolean type PCF control on the Account entity. Based on the Yes / No value of PCF control, we want to create a Contact entity record for the Current Account.

current if the logged-in user has Read/Write/Update access to any specific entity or not
So, when the user is trying to set ‘Yes’ to the ‘Create Contact’ field, we want to first check whether the current user has ‘ to Create’ permission of Contact entity record or not.

Solution

We will achieve this with the help of context.utils.hasEntityPrivilege function that is used to detect whether the current logged-in user has Read/Write permission for a specific entity or not. In our case, we need to check if the active users have ‘to Create’ permission for the Contact entity record. Please find the below code that we have used to find out the same.
context.utils.hasEntityPrivilege(“contact”,1,3)
where,
contact = Entity type name for which we have to find privilege details for the said entity.
1 = It indicates Entity privilege type such as Read, Write, etc. In the above scenario, we check for Create privilege so 1 is associated with ‘Create’. Please find some other Entity type names with their code:
None = 0
Create = 1
Read = 2
Write = 3
Delete = 4
Assign =5
Share =6
Append =7
AppendTo =8
3 = It indicates the entity privilege depth of the specified entity. It has the following values:
None = -1
Basic = 0
Local = 1
Deep = 2
Global = 3
So, by calling this function, we found that the current logged in user has ‘to Create’ permission for Contact entity record as shown in the below screenshot:

current if the logged-in user has Read/Write/Update access to any specific entity or not

As per the result given by context.utils.hasEntityPrivilege function, we take a decision to create a record contact entity record or not.

Conclusion

Using context.utils.hasEntityPrivilege, we can easily retrieve the current logged in user privilege details.

Dynamics 365 Email Alerts