Tuesday, February 4, 2020

Display user profile Images in PowerApps

If you don’t do it right, you will see run-time errors that typically look like this:
Note: These errors only appear at run-time, you will not see them occur when running the PowerApp from the editor in preview mode, however, they will show up in the editor designer, like this.
The Pattern
To display Office 365 profile pictures without throwing errors, you need to do several checks ahead of time to ensure you do not see errors in the PowerApp.
Note: Previously, a bug existed in the connector that returns if a user has a profile picture or not.  Thankfully, the bug has been corrected in the connector, and now this pattern is possible to implement.
Here’s how to go about it.
  1. Check to see if the user has an Id
  2. Check to see if the user is an external user by inspecting the domain for their email account
  3. Check to see if the user has a profile picture
  4. If any of the checks fail, display a placeholder image
  5. If all of the checks pass, call Office365Users.UserPhoto to return the photo and display it 
If(
    IsBlank(ThisItem.Id),
    'profilepic-generic-user',
    If(
       Not(UserDomain.Text = MyDomain),
       'profilepic-generic-user',
       (
        If(
            Office365Users.UserPhotoMetadata(ThisItem.Id).HasPhoto = true,
            Office365Users.UserPhoto(ThisItem.Id),
            'profilepic-generic-user')
           )
       )
   )
Original post: https://toddbaginski.com/blog/how-to-display-office-365-user-profile-images-in-powerapps/

No comments:

Post a Comment