🔵 Solution 1: Power Apps + SharePoint List (Direct Lookup)
🔵 Solution 1: Power Apps + SharePoint List (Direct Lookup)
🟩 Step 1: Create a SharePoint List
- List Name:
UserRoles - Columns:
Email(Single line of text)Role(Choice or Single line of text: Owner, Member, Viewer)
🔸 Example Data:
| Role | |
|---|---|
admin@yourdomain.com |
Owner |
user1@yourdomain.com |
Member |
viewer@yourdomain.com |
Viewer |
🟠Step 2: Connect SharePoint to Power Apps
- In Power Apps, go to the Data tab.
- Click + Add data → Choose SharePoint.
- Connect to your site and select the UserRoles list.
🟣 Step 3: Corrected App OnStart Code
Here’s the fixed and optimized OnStart code:
// Get the current user's email
Set(CurrentUserEmail, Lower(User().Email));
// Retrieve the user’s role from the SharePoint list
ClearCollect(
UserRoleCollection,
Filter(
UserRoles,
Lower(Email) = CurrentUserEmail
)
);
// Check if a role was found and set flags
If(
CountRows(UserRoleCollection) > 0,
Set(UserRole, First(UserRoleCollection).Role),
Set(UserRole, "Viewer") // Default role if no match found
);
// Set access control flags
Set(UserIsOwner, UserRole = "Owner");
Set(UserIsMember, UserRole = "Member");
Set(UserIsViewer, UserRole = "Viewer");
🔧 Fixes applied:
- Case-insensitive email matching: Both sides use
Lower(). - Using ClearCollect: Ensures you handle multiple matches (if any).
- Default role fallback: Sets
Viewerif no role is found. - Direct role check: Sets flags for
Owner,Member, orViewer.
🟢 Step 4: Control Access in Power Apps
- Form Mode (Edit or View):
DisplayMode: If(UserIsOwner || UserIsMember, DisplayMode.Edit, DisplayMode.View)
- Button Visibility (e.g., Submit button):
Visible: UserIsOwner || UserIsMember
- Prevent Unauthorized Submissions:
If(
UserIsOwner || UserIsMember,
SubmitForm(EditForm1),
Notify("You do not have permission to edit this item.", NotificationType.Error)
)
- Show Role to User:
Label.Text: "Your Role: " & UserRole
✅ Final Result:
- Owners: Full edit access
- Members: Limited edit access
- Viewers: Read-only access (default role if not found)
Would you like me to add Power Automate for real-time updates or role expiration checks? Let me know! 🚀✨
Nice solution
ReplyDelete