CRM Crate

How to retrieve user’s security roles in Canvas App?

We will learn to retrieve the user’s security roles in Canvas App. Before we start, make sure to subscribe to CRM Crate so that you stay up-to-date in field of Dynamics 365.

Why do we need to retrieve user security roles in Canvas Apps?

Consider a business scenario where need to perform some dedicated actions depending on the security roles assigned to the logged in user. We can achieve such requirements using the below given implementation.

Build custom logic in Canvas Application.

Step 1 – Retrieve user roles in a collection on the “OnStart” property of Canvas App.

  • Open any existing or a new Canvas App in the Power Apps studio.
  • Navigate to the Canvas App “Tree View” panel and click on “App” component as shown below.
  • Select the “OnStart” event and apply the code expression given below.
ClearCollect(UserRoles, (LookUp(Users,domainname = User().Email).'Security Roles (systemuserroles_association)').Name)
  • A new collection will be created during the OnStart event of an application.
  • Below is the explaination of the above used code expression.

User().Email

This returns the email address of the logged-in user.

LookUp(Users,domainname = User().Email)

Retrieves the user from Office 365 whose email address is equal to logged-in user email address.

‘Security Roles (systemuserroles_association)’),Name

This will return the name of all security roles assigned to the logged-in user.

Step 2 – Display the security roles in Canvas App testing purpose.

  • Since we will be storing the user security roles in a collection, therefore we will use the “Gallery” control to display the list of security roles stored in the collection.
  • In our use-case, we have created a new collection and applied the above created collection in the gallery item as shown below.

Step 3 – Validate the result in the live canvas application.

  • Save and publish the Canvas App to reflect the above made configurations and changes.
  • Open the Canvas App in live mode and validate whether the assigned security roles of the logged-in user are visible in the Canvas App.
List of security roles assigned to the user “Prashant Tirlotkar”.
Clicking the button “Verify Roles” to view the security roles.
The security roles are automatically displayed in the gallery control of the Canvas App.

Thus we learned to retrieve the user’s security roles in Canvas App. Before we start, make sure to subscribe to CRM Crate so that you stay up-to-date in field of Dynamics 365.

3 2 votes
Article Rating
Subscribe
Notify of
9 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Bucheron
8 months ago

No information from the “User” Tabe.

Last edited 5 months ago by Prashant Tirlotkar
HFG
7 months ago

Hi, could you tell me which system table permissions a user needs to be able to do this? The method works perfectly in test with a user who has the system admin role but not for other users. I have given read permissions on the security role and user tables but it still doesn’t work. Any ideas ? Thanks

Aedu
3 months ago

I have the same problem. It is not (only) a problem of license. There must be some other setting(s) in the security role that are missing. Do you know which settings?

Mohamed prince
7 months ago

What a domainname ?

Aedu
3 months ago

Does this also work, if the security role is assigned to a Dataverse Team where the logged in user is a member of, instead of assigning the security role directly to the user?

Last edited 3 months ago by Aedu
error: CRM Crate Security Engine - Disabled Right Click & Selection!

Congratulations!

Well Done,
Welcome to CRM Crate

Stay tuned with us and get all latest updates and learning in Microsoft CRM and related techonologes.