🔵 Solution 1: Power Apps + SharePoint List (Direct Lookup)

🔵 Solution 1: Power Apps + SharePoint List (Direct Lookup)

🟩 Step 1: Create a SharePoint List

  1. List Name: UserRoles
  2. Columns:
    • Email (Single line of text)
    • Role (Choice or Single line of text: Owner, Member, Viewer)

🔸 Example Data:

Email Role
admin@yourdomain.com Owner
user1@yourdomain.com Member
viewer@yourdomain.com Viewer

🟠 Step 2: Connect SharePoint to Power Apps

  1. In Power Apps, go to the Data tab.
  2. Click + Add data → Choose SharePoint.
  3. 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:

  1. Case-insensitive email matching: Both sides use Lower().
  2. Using ClearCollect: Ensures you handle multiple matches (if any).
  3. Default role fallback: Sets Viewer if no role is found.
  4. Direct role check: Sets flags for Owner, Member, or Viewer.

🟢 Step 4: Control Access in Power Apps

  1. Form Mode (Edit or View):
DisplayMode: If(UserIsOwner || UserIsMember, DisplayMode.Edit, DisplayMode.View)
  1. Button Visibility (e.g., Submit button):
Visible: UserIsOwner || UserIsMember
  1. Prevent Unauthorized Submissions:
If(
    UserIsOwner || UserIsMember,
    SubmitForm(EditForm1),
    Notify("You do not have permission to edit this item.", NotificationType.Error)
)
  1. 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! 🚀✨

Comments

Post a Comment

Popular posts from this blog

Bridging the Impossible: Connecting Jira On-Prem to Power Automate & Copilot Studio — The Solution Nobody Built Until Now"

How I Automated My Entire SharePoint Tenant with 150 MCP Tools and Claude Desktop

Azure Management MCP Server