9

I want to be able to get all user's office365 photos within Azure Active directory.

Right now I'm able to get the current user's email using graph SDK

GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();

public async Task<string> GetMyEmailAddress(GraphServiceClient graphClient)
    {          
        User me = await graphClient.Me.Request().Select("mail,userPrincipalName").GetAsync();
        return me.Mail ?? me.UserPrincipalName;
    }

But I'm not sure how to integrate the the getting photos part from https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/profilephoto_get into the code.

Any help or code example is appreciated!!

yfan183
  • 547
  • 2
  • 7
  • 20

4 Answers4

10

This helps to fetch the image

 GraphServiceClient graphServiceClient = GetAuthenticatedGraphServiceClient();

 Stream photo = await graphServiceClient.Me.Photo.Content.Request().GetAsync();

 if (photo != null)
 {
     MemoryStream ms = new MemoryStream();
     photo.CopyTo(ms);
     byte[] buffer = ms.ToArray();
     string result = Convert.ToBase64String(buffer);
     string imgDataURL = string.Format("data:image/png;base64,{0}", result);
     ViewBag.ImageData = imgDataURL;
 }
 else
 {
      ViewBag.ImageData = "";
 }

enter image description here

qJake
  • 16,821
  • 17
  • 83
  • 135
4

In 2021 you can just do:

Stream photo = await graphServiceClient.Me.Photo.Content.Request().GetAsync();
            ViewData["Photo"] = Convert.ToBase64String((photo as MemoryStream).ToArray());

And then in your html:

<img src="data:image/png;base64, @ViewData["Photo"]" />
Kaare Mai
  • 41
  • 2
1

You get the photo using graphClient.Me.Photo.Content which will retrieve the binary data of the photo in a stream:

 public async Task GetPictureAsync()
 {
     GraphServiceClient graphClient = GetGraphServiceClient();

     var photo = await graphClient.Me.Photo.Content.Request().GetAsync();
     using (var fileStream = File.Create("C:\\temp\\photo.jpg"))
     {
         photo.Seek(0, SeekOrigin.Begin);
         photo.CopyTo(fileStream);
     }
 }
RasmusW
  • 3,355
  • 3
  • 28
  • 46
  • Will I still be able to use the Microsoft Graph API if my app is registered in Azure AD endpoint? Because now I'm getting error saying that my app is not supported for this api version – yfan183 Feb 09 '17 at 20:51
  • 1
    Yes. I tested the code above with an app registration that I made in the Azure Portal, The line ``var photo = await graphClient.Me.Photo.Content.Request().GetAsync();`` requests the v1.0 MS Graph API (https://graph.microsoft.com/v1.0/me/photo/$value). – RasmusW Feb 10 '17 at 06:43
  • 2
    I'm getting another strange error saying `Access Denied: Check Credentials and try again` when I'm trying to access a user's photo using `await graphClient.Users["userAddress@email.com"].Photo.Content.Request().GetAsync();` However I can use this to get any other information regarding the user such as `await graphClient.Users["userAddress@email.com"].Request().Select("mail").GetAsync();` to get the user's email – yfan183 Feb 12 '17 at 21:06
-1

If you go to the Graph Explorer : https://developer.microsoft.com/en-us/graph/graph-explorer You can sign in with your email address. You should see a "my photo" query option. There is a documentation link which takes you here: https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/profilephoto_get You'll see a table which indicates that this only works with School or Work accounts - not with your personal Microsoft account.