Monday, 3 May 2021

PowerApps - Get Canvas App Logged-In User Role from Dynamics 365


Hi Everyone,

Hope you are doing great and staying safe!

Today, in this blog post I am going to share #PowerGuideTip31, where I'll demonstrate how to read Dynamics 365 security role in PowerApps Canvas App.

There are many business use cases, where we need to design an App based on the Dynamics 365 security role. One of the best examples is:

Role-based Canvas App, where we need to navigate the user to different-2 screens or perform different process/flow based on the role assigned in the Dynamics 365 application.

I have already been demonstrated this feature in my earlier blog post, In that post, I had used Power Automate to get the Dynamics 365 Security Role in Canvas App. However, today I am going to retrieve it directly in Canvas App without using any external components.

Let's get started...

I have designed a Canvas App that allows -
  • Event Organizers to manage the organization events.
  • Speakers to submit their session.
  • Participants to register for an event.


Step 1: Design a Login Screen

Step 2: Use an expression to retrieve Canvas App logged-in user's security role from Microsoft Dataverse

I am using the following expression (Microsoft Power Fx) on OnStart property of the Canvas App.
That means, every time when you open the app, all security roles assigned to the logged-in user will get automatically stored in the Canvas App Collection.

ClearCollect(UserRoles, (LookUp(Users,domainname = User().Email).'Security Roles (systemuserroles_association)').Name)

Here User().Email - returns the logged-in user email address.

LookUp(Users,domainname = User().Email) - Retrieve User from Office 365 whose email address is equal to logged-in user email address

'Security Roles (systemuserroles_association)'),Namewill return the name of all security roles assigned to the logged-in user.

Finally, I am storing the list of all security roles in Collection, so that I can read it throughout my app.

Important Note: App OnStart event will not trigger when you play/run the app from Power Studio Editor. Hence, you won't see any value stored in the Collection. To do that, you need to manually trigger the OnStart event by clicking on Run OnStart (see below).

Step 3: Restrict Canvas App user's login based on security role

Once you have the security roles of the logged-in user, the next thing you need to write the expression on Login Screen to check whether a user has sufficient privileges to access the app or not.

To do that, I have used the following expression on click of 'Event Organizer' control

If(IsBlank(LookUp(UserRoles,Name="Event Organizer")),Navigate(RoleErrorMessage,ScreenTransition.CoverRight,{rolemissing:"Event Organizer"}),Navigate(HomeScreen))

Explanation: Here, I am checking whether the canvas app collection has an Event Organizer role in the list. If it returns blank, then redirect the user to Error Screen else redirect the user to Home Screen.

And, using the following expression on click of 'Participants control


Explanation: Here, I am checking whether the canvas app collection has a Participant role in the list. If it returns blank, then redirect the user to Error Screen else redirect the user to Home Screen.

Step 4: Test and Demo

Useful Tip

In the above example, I have retrieved the user's security role on the canvas app OnStart event and stored it in the Collection. Afterward, I am using these roles to design a role-based canvas app, However, you might also have a requirement to show the list of logged-in user roles in the Canvas App Screen. To do that, perform the followings steps:

Add a List type of Screen from the Screen List

Set the 'items' property of the list screen to the Collection name (created in Step 2)

I hope you found this PowerGuideTip helpful.

Stay tuned for #PowerGuideTip32

Sunday, 2 May 2021

Canvas Apps - Auto populate Option Set value in the Canvas App Form


Hi Everyone,

Hope you are well and staying safe!

In today's #PowerGuideTip30, I will show you how to auto-populate or set value in Option set field in Canvas App.

I have designed a Canvas App for Event Management purposes, where I have added a screen that the Event Organizer uses to create an Event. This screen has an Option Set field called 'Event Status' that shows the status of the event.

I don't want to allow the Event Organizer to set the Event Status value manually and want to auto-populate its value to 'Submitted' instead.


To auto-populate value in Option Set value or Set value in Option Set value you should use Choices() expression.
  • The Choices function returns a table of the possible values for a lookup column.
  • Use the Choices function to provide a list of choices for your user to select from. This function is commonly used with the Combo box/ Drop Down control in edit forms.


Choices( column-reference )

column-reference – Required. A lookup column of a data source or Option Set Field column. Don't enclose the column name in double-quotes. The reference must be direct to the column of the data source and not pass through a function or control.


Choices('Event Status (Events)'.Submitted)

where 'Event Status (Events)' represents the Event Status column of the Event table in Microsoft Dataverse and Submitted is the Option set value that I want to auto-populate.

Note: I want to auto-populate the option set value, hence I have used the Choices(....) expression directly on the items property of Combo box. If you want to set the value on button click then you can use the same expression on onselect property of button control.

Useful Reference

I hope you found this PowerGuideTip helpful. 

Stay tuned for #PowerGuideTip31

Thursday, 8 April 2021

Quick guide to use PCF Controls (Custom Code Components) in PowerApps Portals


Hi Everyone,

I hope you are well, staying safe and healthy.

Today I am extremely happy to share the recent Microsoft update, that Custom Code Components (PowerApps Component Framework Controls) or PCF Controls are now supported in PowerApps Portals.

For those who do not know about Custom Code Component or PCF Controls, Please bear with me, because In today's PowerGuideTip29, I am going to share a quick guide to use the custom code components in PowerApps Portals.

What is Custom Code Component or PCF Controls?
  • Custom controls let you transform fields that traditionally contain text into visualizations. Similarly, you can use custom controls to transform datasets, such as a view, to display in a more visual rendering rather than a list of records. 
  • Custom controls can appear as visualizations on forms, dashboards, views, and homepage grids. You can set one type of custom control to appear in the web browser client while having a different custom control appear in your Dynamics 365 for Customer Engagement phone or tablet mobile apps. For example, you could use a number input custom control for a field in web browser clients and a slider custom control for the phone app. 
  • After the customization is published, users can fully interact with the control to change the value, such as by sliding the control when using the linear slider custom control. Changes are automatically saved when the form is closed just as they are when the user changes a traditional field on a form.

How many types of Custom Controls are available in the Model-driven app?
  • You can use a rich set of additional controls to create a more touch-friendly experience on model-driven apps. These include sliders, switches, multimedia players, input masks, calendars, and other controls.
  • You can find the list of all OOB Custom Controls here.

Can I create my own Custom Control (PCF Controls)?

Yes, of course, you can create your own Custom Controls as well based on your business needs. There are few PCF Champs out there, who have designed so many useful and wonderful custom controls. You can download these controls and can start using them.

You can find the PCF Gallery here

Check this article for step by step guide to create your own custom controls.

How to use Custom Control in PowerApps Portals?

You need to perform the following steps in order to use the Custom Control in PowerApps Portals.

  • PowerApps License or Trial Instance
  • System Admin rights
  • Portal version must be  9.3.3.x or higher.
  • Your starter portal package must be 9.2.2103.x or higher.
Step 1 - Build and package your code component. Check here
Step 1 - Use the existing custom control

Step 2 - Add the custom control to a field in the Model-driven app

Step 3 - Create Basic Form in Portals
  • For demonstration, I have created a new weblink (My Account) under Profile Navigation Web Link sets and associated it with My Account Web Page. Anf finally I have associated the My Account web page with the My Account Basic form.
  • In My Account Basic Form, I am displaying the portal logged-in user account details. Hence, I have chosen Record Associated to Current Portal User  as RecordSource Type and took form in Edit mode, so that the portal user could update their account details

Step 4 - Create Basic Form Metadata for each Custom Control field in Basic Form

For demonstration, I have used custom controls on the following highlighted fields.

To display the custom control applied on the model-driven app forms field, you need to create a Basic Form metadata for each field and set its Control Style property to 'Code Component' as highlighted below 

Step 5 - Configure Entity Permission for Web Resource table

Associate appropriate Web Role to the Table Permission. I want to display the Custom Controls to all Authenticated Users. Hence I have associated Authenticated Web Roles to the Table Permission.

Step 6 - Clear Portal Cache (or Sync Configuration) and Test Custom Controls on Portals.

Useful Source & References

Hope you found this article helpful.

Stay tuned for the next PowerGuideTip30.
Blogger Widgets