Graph API : How to get the specified profile photo programmatically?

This is one of the regularly asked queries by the Azure/Office 365 developers, how they can get/retrieve the specified profile photo programmatically using Microsoft Graph API.

You can use the userphoto API call to do it. You can get the specified Profilephoto or its metadata (profilephoto properties).

What it does?

  • A GET photo operation first attempt to retrieve the specified photo from Office 365.
  • If the photo is not available in Office 365, the API attempts to retrieve the photo from Azure Active Directory.
  • If the Profilephoto API call is successful, this method returns a 200 OK response code and binary data of the requested photo. If no photo exists, the operation returns 404 Not Found.
  • If the ProfilePhoto API’s get metadata call is successful, this method returns a 200 OK response code and profilePhoto object in the response body.
  • When you use the /photo/$value endpoint to get the binary data for a profile photo, you'll need to convert the data into a base-64 string in order to add it as an email attachment.
  • In case, if you wish to display the image on a web page, create an in-memory object from the image and make that object the source of an image element.
  • Also whenever you use APIs under the /beta version in Microsoft Graph are subject to change. Use of these APIs in production applications is not supported.

Supported Sizes of photo:

The supported sizes of HD photos on Office 365 are as follows: '48x48', '64x64', '96x96', '120x120', '240x240', '360x360','432x432', '504x504', and '648x648'. Photos can be any dimension if they are stored in Azure Active Directory.

You can get the metadata of the largest available photo, or specify a size to get the metadata for that photo size. If the size you request is not available, you can still get a smaller size that the user has uploaded and made available. For example, if the user uploads a photo that is 504x504 pixels, then all but the 648x648 size of photo will be available for download. If the specified size is not available in the user's mailbox or in Azure Active Directory, the size of '1x1' is returned with the rest of metadata.

 

Programmatically test the API call with Microsoft Graph Explorer:

First we try to get metadata, GET https://graph.microsoft.com/beta/me/photo/ Le Cafe de Deva - Deva blogs

 

Then, let me try to get the photo, GET https://graph.microsoft.com/beta/me/photo/$value Le Cafe de Deva - Deva blogs

 

Hope this helps.