{"id":1683,"date":"2018-03-12T14:49:20","date_gmt":"2018-03-12T18:49:20","guid":{"rendered":"https:\/\/toddbaginski.com\/blog\/?p=1683"},"modified":"2018-09-18T14:26:09","modified_gmt":"2018-09-18T18:26:09","slug":"how-to-display-office-365-user-profile-images-in-powerapps","status":"publish","type":"post","link":"https:\/\/toddbaginski.com\/blog\/how-to-display-office-365-user-profile-images-in-powerapps\/","title":{"rendered":"HOW TO: Display Office 365 User Profile Images in PowerApps"},"content":{"rendered":"<p>In almost every PowerApp you develop that integrates with Office 365 you will find yourself displaying Office 365 profile pictures for people.\u00a0 Here are some examples from the PowerApps templates we&#8217;ve created for Microsoft.<\/p>\n<p><strong>Onboarding Tasks PowerApp Template<\/strong><\/p>\n<p><a href=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/get-onboard.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1687\" src=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/get-onboard.jpg\" alt=\"\" width=\"427\" height=\"227\" srcset=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/get-onboard.jpg 427w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/get-onboard-300x159.jpg 300w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/get-onboard-260x138.jpg 260w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/get-onboard-50x27.jpg 50w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/get-onboard-132x70.jpg 132w\" sizes=\"auto, (max-width:767px) 427px, 427px\" \/><\/a><\/p>\n<p><strong>Shoutouts PowerApp Template<\/strong><\/p>\n<p><a href=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/shoutouts.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1688\" src=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/shoutouts.jpg\" alt=\"\" width=\"393\" height=\"562\" srcset=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/shoutouts.jpg 393w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/shoutouts-210x300.jpg 210w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/shoutouts-102x146.jpg 102w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/shoutouts-35x50.jpg 35w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/shoutouts-49x70.jpg 49w\" sizes=\"auto, (max-width:767px) 393px, 393px\" \/><\/a><\/p>\n<h5>The Errors<\/h5>\n<p>If you don&#8217;t do it right, you will see run-time errors that typically look like this:<\/p>\n<p><a href=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/picture-error.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1689\" src=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/picture-error.jpg\" alt=\"\" width=\"1000\" height=\"98\" srcset=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/picture-error.jpg 1000w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/picture-error-300x29.jpg 300w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/picture-error-768x75.jpg 768w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/picture-error-260x25.jpg 260w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/picture-error-50x5.jpg 50w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/picture-error-200x20.jpg 200w\" sizes=\"auto, (max-width:767px) 480px, (max-width:1000px) 100vw, 1000px\" \/><\/a><\/p>\n<p><strong>Note:<\/strong> 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.<\/p>\n<p><a href=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/editor-error.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1690\" src=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/editor-error.jpg\" alt=\"\" width=\"612\" height=\"117\" srcset=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/editor-error.jpg 612w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/editor-error-300x57.jpg 300w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/editor-error-260x50.jpg 260w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/editor-error-50x10.jpg 50w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/editor-error-200x38.jpg 200w\" sizes=\"auto, (max-width:767px) 480px, 612px\" \/><\/a><\/p>\n<h5>The Pattern<\/h5>\n<p>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.<\/p>\n<p><b>Note:\u00a0<\/b>Previously, a bug existed\u00a0in the connector that returns if a user has a profile picture or not.\u00a0 Thankfully, the bug has been corrected in the connector, and now this pattern is possible to implement.<\/p>\n<p>Here&#8217;s how to go about it.<\/p>\n<ol>\n<li>Check to see if the user has an Id<\/li>\n<li>Check to see if the user is an external user by inspecting the domain for their email account<\/li>\n<li>Check to see if the user has a profile picture<\/li>\n<li>If any of the checks fail, display a placeholder image<\/li>\n<li>If all of the checks pass, call Office365Users.UserPhoto to return the photo and display it<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>Here&#8217;s what this looks like in code:<\/p>\n<p><a href=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/profile-image-code-checks.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1693\" src=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/profile-image-code-checks.jpg\" alt=\"\" width=\"693\" height=\"309\" srcset=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/profile-image-code-checks.jpg 693w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/profile-image-code-checks-300x134.jpg 300w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/profile-image-code-checks-260x116.jpg 260w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/profile-image-code-checks-50x22.jpg 50w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/profile-image-code-checks-380x170.jpg 380w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/profile-image-code-checks-157x70.jpg 157w\" sizes=\"auto, (max-width:767px) 480px, 693px\" \/><\/a><\/p>\n<p>Here&#8217;s the code if you want to copy and paste.<\/p>\n<pre>If(\r\n\u00a0\u00a0\u00a0 IsBlank(ThisItem.Id),\r\n\u00a0\u00a0\u00a0\u00a0'profilepic-generic-user',\r\n\u00a0\u00a0\u00a0\u00a0If(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Not(UserDomain.Text = MyDomain),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 'profilepic-generic-user',\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 If(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Office365Users.UserPhotoMetadata(ThisItem.Id).HasPhoto = true,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Office365Users.UserPhoto(ThisItem.Id),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'profilepic-generic-user')\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 )\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 )\r\n\u00a0\u00a0 )<\/pre>\n<h5>[UPDATE 6-20-2018]<\/h5>\n<p>When you call Office365User.UserPhotoMetadata you can pass in the User.Id or User.UserPrincipalName returned from Office365Users.SearchUser.<\/p>\n<p>At this time, the\u00a0Office365User.UserPhotoMetadata function does not support passing in the User.Mail field returned from Office365Users.SearchUser.\u00a0 This may fail if a user&#8217;s email address is different than their user principal, so it is not recommended to pass email addresses into\u00a0Office365User.UserPhotoMetadata.<\/p>\n<h5>[\/UPDATE]<\/h5>\n<h5>Sample PowerApp<\/h5>\n<p>To demonstrate this, my buddy\u00a0<a href=\"https:\/\/www.linkedin.com\/in\/alexanderbelikov\/\" target=\"_blank\" rel=\"noopener\">Alex Belikov<\/a> and I created a nice little PowerApp that shows two different galleries of people and their Office 365 profile pictures.<\/p>\n<p>The gallery on the left includes all the checks.\u00a0 The gallery on the right does not.\u00a0 In both cases shown below, I am returning internal and external users who do and do not have a profile picture.<\/p>\n<p>Notice, when I refresh the list of people in the gallery on the left, no error messages appear.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1920\" src=\"https:\/\/toddbaginski.com\/blog\/images\/Profile-Pics-Works.gif\" alt=\"\" width=\"1920\" height=\"1080\" \/><\/p>\n<p>However, when I refresh the gallery on the right, an error message appears because the external user does not have a profile picture.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1920\" src=\"https:\/\/toddbaginski.com\/blog\/images\/Profile Pics Errors.gif\" alt=\"\" width=\"1920\" height=\"1080\" \/><\/p>\n<p>You can download the sample PowerApp <a href=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2018\/03\/Office365UserProfileImages_20180312175211.zip\">here.<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In almost every PowerApp you develop that integrates with Office 365 you will find yourself displaying Office 365 profile pictures for people.\u00a0 Here are some examples<span class=\"excerpt-hellip\"> [\u2026]<\/span><\/p>\n","protected":false},"author":1,"featured_media":1710,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[103,96,46,104,121],"tags":[10,5,122,54,45,112],"class_list":["post-1683","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud","category-mobile","category-most-popular","category-office-365","category-powerapps","tag-custom-development","tag-helpful-code","tag-how-to-powerapps","tag-mobile","tag-o365","tag-powerapps"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts\/1683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/comments?post=1683"}],"version-history":[{"count":14,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts\/1683\/revisions"}],"predecessor-version":[{"id":1754,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts\/1683\/revisions\/1754"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/media\/1710"}],"wp:attachment":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/media?parent=1683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/categories?post=1683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/tags?post=1683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}