Code apps allow developers to bring Power Apps capabilities into custom web applications built in a code-first IDE. You can develop the application locally and run the same app within the Power Platform.

To get started refer to Quickstart: Create a code app from scratch

In this post, I will share sample code that checks whether the logged-in user has a specific security role.

Prerequisites:

Your Code App must have the following Dataverse tables added as data sources:

  • Users
  • User Roles
  • Roles

Use following commands:

pac code add-data-source -a dataverse -t systemuserroles
pac code add-data-source -a dataverse -t role
pac code add-data-source -a dataverse -t systemuser

How to use the sample code:

  • You can pass either the user’s email or GUID as the first parameter, and the security role name as the second parameter to check against.
  • The function performs three sequential Dataverse queries:
    • Resolve the user — queries systemusers with the provided email or GUID to get the systemuserid
    • Get role assignments — queries systemuserrolescollection filtered by systemuserid to get all assigned roleid values
    • Match role names — queries roles filtered by the collected role IDs and does a case-insensitive comparison against targetRoleName
  • Refer the Readme file for more details

🙂

Advertisements
Advertisements

Leave a comment