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.

5 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
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.

Practise EXams

Free!!

Sharpen your skills with our free practise exams.