In one of my previous articles, I provided the Power Fx formula, to Check if the logged in User has a specific set of security roles.
However, the Power Fx formula mentioned in the article only fetches and matches the user’s direct security roles, and does not include the team’s security roles where the user is a member.
To fetch both the security roles of the User and the security roles of all the teams where the current user is a member, you can use the following formula.
- Make sure you add ‘Users’, ‘Teams’ and ‘Security Roles’ data sources to your Canvas App.
// Define the role names. Change the role names as per your requirement.
Set(
RoleNames,
[
"Basic User",
"System Administrator"
]
);
// Fetch the current user security roles into 'CurrentUserRoles' collection
ClearCollect(
CurrentUserRoles,
(LookUp(
Users,
domainname = User().Email
).'Security Roles (systemuserroles_association)').Name
);
// Fetch the current user teams
ClearCollect(
CurrentUserTeams,
LookUp(
Users,
domainname = User().Email
).'Teams (teammembership_association)'
);
// Store the user Teams in to temporary collection
ClearCollect(
tempUserTeams,
CurrentUserTeams
);
// Loop through Teams and fetch team's role names
ForAll(
CurrentUserTeams,
Collect(
CurrentUserRoles,
(LookUp(
CurrentUserTeams,
'Team Name' = First(tempUserTeams).'Team Name'
).'Security Roles').Name
);
Remove(
tempUserTeams,
First(tempUserTeams)
);
);
// Remove the duplicate roles
ClearCollect(
DistinctCurrentUserRoles,
Distinct(
CurrentUserRoles,
Name
)
);
// Match the defined 'RoleNames' with 'CurrentUserRoles'
// Set the 'flagUserHasValidRoles' flag with true/false
Set(
flagUserHasValidRoles,
CountRows(
Filter(
DistinctCurrentUserRoles,
Value in RoleNames
)
) > 0
);
- Above formula produces ‘flagUserHasValidRoles’ Boolean variable. If its ‘true’, the logged in user has one of the specified roles. If its ‘false’, logged in user not having any of the specified roles.
🙂

![[Step by Step] Beginner : Create a PCF control and add it to a custom page](https://rajeevpentyala.com/wp-content/uploads/2024/12/image-49.png)

Leave a comment