Dataverse Security Roles in Canvas App
In this blog post, we will explore how to use Dataverse Security Roles in a Canvas App. We will be using the “Users” table and the “LookUp” function to get the current user record and their associated security roles. We will also use the “in” function to check if the user has the “System Administrator” role and set this information in an isAdmin
variable.
Adding the Users Table
- Open your Canvas app in the Power Apps Studio.
- Go to the View tab and click on Data sources.
- In the Data pane that appears on the right, click on Add data.
- In the new window, search for and select Dataverse.
- You’ll see a list of tables. Search for the Users table and click on it to add it to your app.
- In my scenario, The "Users" table is renamed as "Employees"
Now, the Users table is added to your Canvas app and you can use it to look up user information and their associated security roles. Remember to save your changes
Getting the Current User Record
First, we need to get the current user record. We can do this by using the “LookUp” function with the “Users” table and the ‘Primary Email’ field. Here’s how you can do it:
Set(CurrentUser, LookUp(Users, 'Primary Email' = User().Email))
This will set the CurrentUser
variable to the record of the current user.
Getting the Associated Security Roles
Next, we need to get the security roles associated with the current user. We can do this by using the “LookUp” function again, but this time with the ‘Security Roles’ field:
Set(UserRoles, CurrentUser.
'Security Roles (systemuserroles_association)'.Name)
This will set the UserRoles
variable to the names of the security roles associated with the current user.
Checking for the “System Administrator” Role
Now that we have the user’s security roles, we can check if they have the “System Administrator” role. We can do this by using the “in” function:
Set(isAdmin, "System Administrator" in UserRoles)
This will set the isAdmin
variable to true
if the “System Administrator” role is in the user’s security roles, and false
otherwise.
Conclusion
By using the “Users” table and the “LookUp”, “in” functions in Power Apps, we can easily get the current user’s record and their associated security roles. We can also check if they have a specific role, such as the “System Administrator” role. This can be useful for controlling access to certain parts of your app based on the user’s roles.
Thanks for share your blog here .
ReplyDeleteDynamics 365 Marketing Functional Consultant Associate
My Pleasure
Delete