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().EmailThis 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)’),NameThis 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.
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.