PowerApps · SharePoint

Choosing your path in PowerApps

Often, there are times that you need to present completely different screens and data to the same logged in user but do not want the hassle of having to build multiple apps to accomplish this. One way that I have found to do this is by using variables with app OnStart rules. As you may or may not know, you have the ability to create multiple screens to be used throughout your app. You can use these variable to show/hide data based on the logged in user.

To better understand this, let’s look at the following scenario. I am both a faculty and staff member for my organization. As you can imagine, my job duties are completely different based on my assignment so when I login to my PowerApp I need to see specific data based on a current “role”. To keep it simple I will refer to these roles going forward as “Faculty” and “Staff”.

Let’s start off by creating a blank canvas app. In order to make this work, you will need to make use of the Office365Users data connection to return the logged in user properties along with the data source you are using to submit data to. In my case I am using a combination of SharePoint lists for all of my data sources.

SharePoint list names with columns to be created:
FacultyAdvisors: FacultyName, FacultyEmail
GeneralStaff: StaffName, StaffEmail

In Tree view, click “App” at the top of all of your screens and click the “OnStart” property.

Before I begin setting my variables for the app, I create different Collections to hold my list data so that my PowerApp is not constantly querying SharePoint for data. In the formula bar, create the variables for your collections. In my app I am using the Concurrent function to run as these are collections are not dependent on each other and can load in any order.

***Note: Concurrent works great for instances like this but be careful that you don’t use when rules rely on each other in succession. Made that mistake 🙁

I like using notes throughout my code because let’s face it, I’ll forget what this step is for within 10 minutes of writing it not to mention 6 months from now when I need to review something. With all that being said, let’s start adding our OnStart rules.

//This statement creates collections for my SharePoint list data sources
Concurrent(ClearCollect(collectFaculty, FacultyAdvisors), ClearCollect(collectStaff, GeneralStaff));

Next, I begin setting my variables that will capture the logged in users’ email address and will be used to determine what “type” of user this person is.

//Set the value for the current logged in user
Set(varUser, {MyProfile: Office365Users.MyProfile().Mail});
//This statement sets the variable for Faculty if their email exists in that list
Set(varFaculty, If(varUser.MyProfile in collectFaculty.Email, true,false));
//This statement sets the variable to the value of the FacultyName column in the Faculty SharePoint list
Set(varFacName, LookUp(collectFaculty, Email = varUser.MyProfile).FacultyName);
//This statement sets the variable to the value of the Email column in the FacultyAdvisors SharePoint list
Set(varFacEmail, LookUp(collectFaculty, Email = varUser.MyProfile).FacultyEmail);
//This statement sets the user for Staff if their email exists in that list
Set(varStaff, If(varUser.MyProfile in collectStaff.Email, true,false));
//This statement sets the variable to the value of the StaffName column in the GeneralStaff SharePoint list
Set(varStaffName, LookUpcollectStaff, Email = varUser.MyProfile).StaffName);
//This statement sets the variable to the value of the Email column in the GeneralStaff SharePoint list
Set(varStaffEmail, LookUp(collectStaff, Email = varUser.MyProfile).StaffEmail);

If you notice, I am using “IF” statements to filter the Faculty list and a LookUp to verify if the user account exists or not within the SharePoint lists. The reason behind this is to determine what screens need to be shown to the current logged in user. The next set of blocks is where the magic happens. The first block is essentially an IF/ELSE statement that is used to determine when screen to navigate the logged in user to. I have created a screen named scrnFacStaff that has buttons that will allow for the logged in user to choose which set of screens they would like to be presented with.

//If both Faculty AND Staff navigate and choose your path
If(varFaculty && varStaff, Navigate(scrnFacStaff,ScreenTransition.Fade),
//Else if Faculty or Staff navigate to Home
varFaculty Or varStaff, Navigate(scrnHome,ScreenTransition.Fade));

The next block is used for anyone that does not exist as a Faculty or Staff member. This will navigate the logged in user to a screen with an automatic redirect away from the app to a web page.

//If neither Faculty or Staff
If(varFaculty Or varSraff, false, Navigate(scrnNonFacStaff, ScreenTransition.Fade))

I have begun to incorporate this small amount of code into most of my apps now. This really does come in handy if you are trying to cut down on creating new apps for specific view when you can control all of that using screens and variables.

Have a great day and remember to stay Salty!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.